【为什么我可以在Java中抛出null?】教程文章相关的互联网学习教程文章

java-在IoC容器中将依赖项设置为NULL并在运行时提供依赖项是一种不好的做法吗?【代码】

我有一个SocketManagerclass,其中包含Socket和其他字段.在使用DI框架组成对象图的过程中,可以插入除Socket之外的所有字段.我的想法是通过将Socket保留为空并在运行时进行设置来简单地预先构建整个对象图.这将使我能够在代码中的某一点完成SocketManager实例化,并在整个程序中使用该实例(因为它已通过DI框架设置为依赖项)?这是“注入”运行时依赖项的标准方法还是不好的做法?抽象工厂似乎不是一个好主意,原因有两个:a)每次都会创...

java-Firefox浏览器未加载并显示错误:typeError:addon为null【代码】

public class firstTest {public static void main(String[] args) {System.setProperty("webdriver.gecko.driver", "E:\\SOFTWARE\\drivers\\geckodriver.exe");WebDriver driver = new FirefoxDriver();driver.get("http://www.google.com");} }执行上述代码时,出现以下错误:Unable to read VR Path Registry from C:\Users\DEEPAK D P\AppData\Local\openvr\openvrpaths.vrpath JavaScript error: resource://gre/modules/addo...

readline()在Java中返回null【代码】

我正在尝试在Java程序中阅读标准输入.我期望一系列数字后跟换行符,例如:6 9 1 当通过eclipse内置控制台提供输入时,一切都会顺利进行.但是,使用Windows命令行时,程序将输出:Received '6'. Received 'null'. Invalid input. Terminating. (This line is written by another function that does an Integer.parseint()). 我的代码是:static String readLineFromStdIn(){ try{ java.io.BufferedReader stdin = new jav...

java-Android中的LocationManager的位置返回null【代码】

我正在设备上测试我的Android应用,当我使用它时,它总是会失败locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);但是,如果我使用NETWORK_PROVIDER而不是GPS_PROVIDER,它将起作用. currentLocation返回null. 我该如何处理?我需要在locationListener中添加什么特定内容吗? 谢谢!解决方法:G...

java-Spring Hibernate @autowired在ConstraintValidator中为null【代码】

我已经搜索并找到了关于此问题的很多主题,但是无论出于何种原因,解决方案都无法解决我的问题. 我正在运行Spring Boot 1.5.8和Hibernate. 我创建了一个自定义验证批注,以检查电子邮件与数据库相比是否唯一.批注使用@autowired存储库查询数据库,以检查数据库中是否已存在电子邮件.我不断收到@autowired bean为null的异常. 我的理论是使用休眠验证器而不是UniqueEmailValidator类,因此未正确连接.但是我不知道如何解决这个问题.@Docum...

java-传递null时选择哪个构造函数?【代码】

在下面的示例中,我有2个构造函数:一个构造函数使用String,另一个构造函数使用自定义对象.在此自定义对象上,存在方法“ getId()”,该方法返回String.public class ConstructorTest {private String property;public ConstructorTest(AnObject property) {this.property = property.getId();}public ConstructorTest(String property) {this.property = property;}public String getQueryString() {return "IN_FOLDER('" + property...

java-Kotlin中的错误“必须不为null”【代码】

我正在尝试获取一个.zip文件中的多个文件.尝试解压缩文件会提供java.lang.IllegalStateException:zis.nextEntry不能为null.如何正确地做呢?@Throws(IOException::class)fun unzip(zipFile: File, targetDirectory: File) {val zis = ZipInputStream(BufferedInputStream(FileInputStream(zipFile)))try {var ze: ZipEntryvar count: Intval buffer = ByteArray(8192)ze = zis.nextEntrywhile (ze != null) {val file = File(targ...

java-使用BoxLayout动态扩展JPanel(在null布局上)【代码】

我有一个带有垂直BoxLayout的JPanel,在带有空布局的JPanel之上. 我希望带有BoxLayout的JPanel随着组件的添加而增长. 参见以下代码:public static void main (String[] args) {JFrame f = new JFrame();f.setSize(500,500);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel total = new JPanel();total.setLayout(null);total.setSize(f.getWidth(),f.getHeight());total.setBackground(Color.green);JPanel box = new JP...

java-setPanel(null)时,JPanel上的组件未显示【代码】

有人可以告诉我为什么组合框不显示吗?我有一个控制器:public class TestController extends JPanel {TestView cgView;public TestController() {setLayout(null);cgView=new TestView();add(cgView);} public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {public void run() {JFrame fr = new JFrame("testt");fr.setSize(1200,1000);fr.setResizable(false);TestController cgc=new TestCont...

java – this.getClass().getClassLoader().getResourceAsStream总是返回null【代码】

在阅读Web应用程序中的文件时this.getClass().getClassLoader().getResourceAsStream(../abc.txt)总是返回null.任何人都可以提供详细信息,我应该把abc.txt放在哪里阅读.关于getResourceAsStream的任何文章都会有所帮助.我搜索了很多,但没有得到任何相关信息. 请提供宝贵的建议.谢谢.解决方法:如果您的abc.txt在classpath中,并且在com / test / oops / testpaper / abc.txt等不同的包中. 然后阅读如下.InputStream io=this.getClass...

java – 为什么更改用作HashMap中的键的对象的哈希码会使查找返回null?【代码】

请考虑以下情形:Object o1 = new Object(); Object o2 = new Object();HashMap<Object, Object> map = new HashMap<Object, Object>(); map.put(o1, o2);boolean test1 = map.get(o1) == o2; // This evaluates to true// Now lets say we alter the state of o1: o1.setSomeInternalState(Object newState);boolean test2 = map.get(o1) == o2; // This evaluates to false, because now map.get(o1) returns null假设o1的类已重...

Java:Null Pointer Deque Iterator【代码】

我被指派编写一个以中缀表示法采用数学表达式的类,并将该表达式转换为后缀表示法中的等效表达式.那部分我已经完成了. 我还被分配编写一个迭代器,让客户端代码迭代后缀表达式的标记. 所以,到目前为止我的迭代器代码是:class PostfixIterator implements Iterator<String> { //this is line 117private Deque<String> postfix;public PostfixIterator(Deque<String> postfix) {this.postfix = postfix; }public boolean hasNext() {...

java – 从BufferedReader(readLine)读取返回null?【代码】

我目前正在尝试从BufferedReader读取一个字符串,但无法找到一种方法来执行此操作… 我当然试过了BufferedReader inStream = null; inStream = new BufferedReader(new InputStreamReader(client.getInputStream())); String test = inStream.readLine();但是,即使BufferedReader inStream等于某种消息,尝试打印到屏幕时结果也为null.解决方法:基于the documentation,BufferedReader.readLine()仅在到达流的末尾时才返回null.这意味...

java – Hibernate将NULL值粘贴到列表中【代码】

我继承了一些使用Hibernate的Java代码.使用此代码的一些人现在报告说他们在整个地方都获得了NullPointerExceptions. 我已经能够跟踪它了,发现当我们执行一个从数据库中提取对象列表的查询时,它有一个对象列表(从另一个表中提取)Hibernate似乎在list(NULL值).所以列表可能看起来像这样:Object Object NULL Object我们用来从数据库中提取信息的代码是:List<PrinterGroup> groups = this.getSession().createQuery( "from PrinterGr...

Spring Boot Security – java.lang.IllegalArgumentException:无法传递null GrantedAuthority集合【代码】

我是Spring boot和Spring Security的新手.我目前的Spring启动版本1.3.7.RELEASE有什么问题吗?我该如何解决这个问题? 我的SecurityConfig.java如下所示:@Configuration @EnableGlobalMethodSecurity(securedEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredpublic void configureAuth(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withU...