Results 1 to 3 of 3
  1. #1
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default 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)

    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(){
    	
    	}
    }
    What is causing this error?:confused:
    Please help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:
    Java Code:
    Scanner checker = new Scanner(findNoOfLines);
    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.

    To prove the point, run this:
    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);
      }
    
    }
    I suggest that you read the JFileChooser API and tutorial to see how to display it and use it.

    for e.g.,
    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);
        }
      }
    }
    Much luck!
    Last edited by Fubarable; 01-13-2010 at 01:13 AM.

  3. #3
    Rohaan is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2010, 12:49 PM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  4. Replies: 4
    Last Post: 04-17-2009, 01:17 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •