Tag: nullpointerexception

JTextField自动完成错误

在这个post中 ,我找到了一种在JTextField (和JComboBox实现AutoCompletefunction的方法,但没有问题。 尝试在JTextField实现此function时,它会抛出一些exception。 以下代码将演示此问题。 它非常简单,与我使用JComboBox的方式很相似,它工作得很好。 有人可以帮我找到解决方案。 这是代码: ( 请注意我使用的是swingx ) import javax.swing.JList; import org.jdesktop.swingx.JXFrame; import org.jdesktop.swingx.JXTextField; import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; public class ErrorDemo { public static void main(String[] args) { JXFrame frame = new JXFrame(“Error Demo”); JXTextField field = new JXTextField(); String[] users = {“aaa”, “bbb”, “ccc”}; AutoCompleteDecorator.decorate(new JList(users), field); frame.getContentPane().add(field); frame.pack(); frame.setLocationByPlatform(true); frame.setDefaultCloseOperation(JXFrame.EXIT_ON_CLOSE); frame.setVisible(true); } […]

Java的。 可能抛出的params(NullpointerException)的函数?

当我有许多可以抛出exception的表达式时,例如: instanceObj.final_doc_type = instance.getFinalDocument().getValue().getType().getValue(); instanceObj.final_doc_date = instance.getFinalDocument().getValue().getDate().toGregorianCalendar().getTime(); instanceObj.appeal_date = instance.getFinalDocument().getValue().getAppealDate().getValue().toGregorianCalendar().getTime(); … instanceObj.start_doc_type = instance.getStartDocument().getValue().getDocType().getValue(); instanceObj.apeealed_type = instance.getStartDocument().getValue().getApeealedType().getValue(); instanceObj.declarers_list_mult_id = instance.getStartDocument().getValue().getDeclarers().getValue().getString(); … 是否有任何方法来处理这些表达式由某个函数将返回一些默认值(或null)如果参数无效并引发exception – 这可能发生,例如: instance.getFinalDocument().getValue().getDate() = null 因此,我不需要使用try-catch块包围每个表达式或检查每个点是否为null。

抛出空指针exception

我在android中做一个应用程序,我需要访问com.android.internal.telephony API。 现在我能够访问这些API,但问题是我在自己的类中调用Class Call.java的getEarliestConnection()方法,它抛出了NullPointerException 。 你可以在http://hi-android.info/src/com/android/internal/telephony/Call.java.html找到Call.java。 在这个类中有以下方法: 1. public Connection 2. getEarliestConnection() { 3. List l; 4. long time = Long.MAX_VALUE; 5. Connection c; 6. Connection earliest = null; 7. 8. l = getConnections(); 9. 10. if (l.size() == 0) { 11. return null; 12. } for (int i = 0, s = l.size() ; […]

当我对我之前在我的代码中执行的相同WebElement执行click()时,我得到nullpointerexception

我在Java和Selenium上得到了很好的帮助。 当我使用相同的“Input_Search_Box”Webelement来执行click方法时,它会抛出一个nullpointerexception。 我已经谷歌搜索并尝试了一些工作,如添加线程,添加显式等待但仍然没有线索,我想念。 任何建议将不胜感激。 这是我的代码: public class Testclass { WebElement Input_Search_Box; WebDriver driver; @Test public void openBrowser() throws Exception{ System.setProperty(“webdriver.chrome.driver”,”E:\\Ecilpse\\chromedriver_win32\\chromedriver.exe”); driver = new ChromeDriver(); driver.get(“https://en.wikipedia.org/wiki/Main_Page”); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); driver.manage().window().maximize(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript(“window.scrollBy(0,500)”); WebElement Click_Create_Book = driver.findElement(By.xpath(“.//*[@id=’coll-create_a_book’]/a”)); Click_Create_Book.click(); WebElement Start_Book_Creator_Btn = driver.findElement(By.xpath(“.//*[@id=’mw-content-text’]/form/div/div[1]/button”)); Start_Book_Creator_Btn.click(); Input_Search_Box = driver.findElement(By.xpath(“.//*[@id=’searchInput’]”)); Input_Search_Box.click(); Input_Search_Box.sendKeys(“Selenium”,Keys.ENTER); for(int i =0;i<=8;i++){ try{ if(driver.findElement(By.xpath(".//*[@id='siteNotice']/div[2]/div[2]/div")).isDisplayed()) break; } catch(Exception […]

引用变量到Java中另一个类中实例化/初始化的对象

我问的原因是因为我得到了NullPointerException。 我现在这很容易,但我很新编程,发现这有点令人困惑。 所以说我已经在类中初始化了一个对象,并希望从另一个类访问该对象。 比如现在我正在做一个小型国际象棋游戏,在我的模型游戏类中,我有一个Board实例,一个对象。 反过来,董事会有一系列Squares。 广场[][]。 游戏有棋盘,棋盘有Square [] []。 现在,如果我想通过Board类型的对象板(在游戏中)访问Square [] []。 我只是声明一个具有相同名称和类型的变量,或者我是否必须再次初始化它? 董事会或董事会董事会=新董事会(); 注意,我已经在类Game中初始化了板,所以如果我再次执行它,它们不会是两个完全不同的Board对象吗? 引用“板”的类: public class View extends JFrame { Board board; JFrame gameWindow = new JFrame(“Chess”); JPanel gamePanel = new JPanel(); JPanel[][] boardPanel = new JPanel[8][8]; JMenuBar gameMenu = new JMenuBar(); JButton newGame = new JButton(“New game”); JButton pauseGame = new JButton(“Pause”); […]

NullPointerException:println需要一条消息

我知道我有一个空指针,但我不知道它的发生位置或原因。 希望能在这里得到一些帮助。 Button add = (Button) findViewById(R.id.addfencebutton); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View add) { //code to store in fence locations database createData(); } }); } public void createData(){ Course course = new Course(); course.setcoursename(“courseNamefield”); course.setfencename(“fenceNamefield”); course.setAlocation(“standardAfield”); course.setBlocation(“standardBfield”); course = datasource.create(course); Log.i(course.getcoursename(), null); //Log.i(“course created with id” + course.getcourseid(), null); // Log.i(“course name is […]

当我使用autowire和组件注释时,不会注入或创建我的spring bean

我正在尝试理解DI,但是当我使用autowire和组件注释时,我的spring bean没有被注入或创建。 相反,我得到一个nullpointerexception,因为我的bean是null。 我应该手动创建它,在那种情况下? 当几个bean相互依赖时,您在哪里指定创建bean的顺序? 这是我的代码: App.java package se.springtest; import se.springtest.Person; import org.springframework.core.io.ClassPathResource; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; public class App { public static void main( String[] args ) { BeanFactory factory = new XmlBeanFactory( new ClassPathResource(“application-context.xml”)); Person p = (Person) factory.getBean(“person”); p.show(); } } 应用程序的context.xml Person.java package se.springtest; public interface Person { public void show(); […]

“线程中的exception”AWT-EventQueue-0“java.lang.NullPointerException”。 我不知道我做错了什么

我正在为AP计算机科学决赛制作计算器。 我使用Jigloo在Eclipse中构建了GUI,我很快就尝试了解Action Listeners,这样你就可以点击按钮来显示数字。 当我开始输入实际代码进行计算时,问题就开始出现了。 我一直收到以下错误: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException at NewJFrame.(NewJFrame.java:82) at NewJFrame$1.run(NewJFrame.java:73) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown […]

初始化2D对象数组时出现空指针exception

我正在尝试制作2D平铺游戏,当制作包含贴图的数组时,我得到一个NullPointerException,这里有一些代码。 对不起,如果这是格式不好,第一个计时器 公共课世界{ //holds data for where to place images and rectangles int[][] worldDat = new int[25][25]; //hold rectangles for checking interaction with player Rectangle[][] blocks = new Rectangle[25][25]; //holds block’s images to be painted Image[][] blockImage = new Image[25][25]; //holds position to be pained on screen int[][] location = new int[25][25]; //enumeration holding block’s […]

使用File.listfiles()获取NullPointerexception

所以我的Android Gallery中有3个文件夹。 我试图将这些文件夹的文件读入文件数组,然后从那里将图像源转换为实际的ImageViews 。 我的问题是我在引用我的File数组时遇到NullPointerException 。 这是一些代码: // Get image files paths. private File topsFolder = new File(Environment.getExternalStorageDirectory().getPath() + “/DressIt Tops/”); private File bottomsFolder = new File(Environment.getExternalStorageDirectory() .getPath() + “/DressIt Bottoms/”); private File shoesFolder = new File(Environment.getExternalStorageDirectory() .getPath() + “/DressIt Shoes/”); private File[] allTops = topsFolder.listFiles(); private File[] allBottoms = bottomsFolder.listFiles(); private File[] allShoes = […]