Results 1 to 3 of 3
- 01-13-2010, 01:01 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
Hi
whenever I try to compile and run the following code, i get this error
Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at animalmain.main(animalmain.java:24)
What is causing this error?:confused:Java Code:import java.util.Scanner; import javax.swing.*; public class animalmain { public static void main(String[] args) throws Exception { int noOfLines = 0; int b = 1; int ask; boolean win = false; String lineSeparator = System.getProperty("line.separator"); JFileChooser filechooser = new JFileChooser(); java.io.File findNoOfLines = filechooser.getSelectedFile(); Scanner checker = new Scanner(findNoOfLines); checker.useDelimiter(lineSeparator); while (checker.hasNext()){ noOfLines++; } checker.close(); questionarray[]quest = new questionarray[noOfLines]; for(int i = 0; i < quest.length; i++){ quest[i] = new questionarray(); } java.io.File file = filechooser.getSelectedFile(); Scanner reader = new Scanner(file); reader.useDelimiter("|"); while (reader.hasNext()){ int i = reader.nextInt(); quest[i].q = reader.next(); quest[i].yes = reader.nextInt(); quest[i].no = reader.nextInt(); } reader.close(); while (b <= quest.length){ ask = JOptionPane.showConfirmDialog(null, quest[b].q, "Guess 1.0", JOptionPane.QUESTION_MESSAGE); if (ask == JOptionPane.YES_OPTION) b = quest[b].yes; else b = quest[b].no; if(b == -1){ win = true; break; } else if (b == -2){ win = false; break; } } if (win == true){ JOptionPane.showMessageDialog(null, "I win"); } else if (win == false){ java.io.PrintWriter toFile = new java.io.PrintWriter(file); Scanner writeToFile = new Scanner(file); String write = JOptionPane.showInputDialog("Enter the animal you were thinking of"); String writer = JOptionPane.showInputDialog("Enter a question that you could answer yes for " + write + " and no for " + quest[b].q); while (writeToFile.hasNext()){ String s1 = writeToFile.nextLine(); String s2 = s1.replaceAll(quest[b].q + writeToFile.nextInt() + writeToFile.nextInt(), writer + (quest.length+1) + (quest.length+2) ); } toFile.print((quest.length+1) + "|" + quest[b].q + "|"); toFile.print(-1); toFile.print("|"); toFile.println(-2); } } } class questionarray{ int qnum; String q; int yes; int no; questionarray(){ } }
Please help
-
The NullPointerException (NPE) tells you which line is null, and so you know where to look for the problem. I can tell you that it will trip here:
and since checker can't be null, it must be findNoOfLines. This is most certainly null since you're asking the JFileChooser object to return a file before you've even displayed it.Java Code:Scanner checker = new Scanner(findNoOfLines);
To prove the point, run this:
I suggest that you read the JFileChooser API and tutorial to see how to display it and use it.Java Code:import javax.swing.*; public class Fu2 { public static void main(String[] args) throws Exception { JFileChooser filechooser = new JFileChooser(); java.io.File findNoOfLines = filechooser.getSelectedFile(); //Scanner checker = new Scanner(findNoOfLines); System.out.println(findNoOfLines); } }
for e.g.,
Much luck!Java Code:import javax.swing.*; public class Fu2 { public static void main(String[] args) throws Exception { JFileChooser filechooser = new JFileChooser(); int result = filechooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { java.io.File findNoOfLines = filechooser.getSelectedFile(); System.out.println(findNoOfLines); } } }Last edited by Fubarable; 01-13-2010 at 01:13 AM.
- 01-15-2010, 01:30 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException
By Borysator in forum New To JavaReplies: 2Last Post: 01-10-2010, 12:49 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
Exception in thread "main" java.lang.NullPointerException
By svpriyan in forum New To JavaReplies: 4Last Post: 04-17-2009, 01:17 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks