Results 1 to 5 of 5
  1. #1
    ryanonnfire54 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Unknown NullPointerException Error...

    Hello again everyone,

    I am stumped. It may be a lack of my understanding for ArrayLists, but I get a runtime NullPointerException. I have a voter GUI set up, and when the "LogOn" button is clicked, you enter an ID. The ID (variable 'user') is then sent to the LogOn class to be checked. The error, as denoted by the console is marked in the code here:

    LogOn class:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    import javax.swing.JOptionPane;
    
    
    public class LogOn {
    	
    	ArrayList<ArrayList<String>> voterMaster;
    	int indexOfVoter;
    	
    	public LogOn(){
    		
    		Scanner sc;
    		try {
    			sc = new Scanner(new File("voters.txt"));
    			
    			ArrayList<ArrayList<String>> voterMaster = new ArrayList<ArrayList<String>>();
    			ArrayList<String> voter = null;
    			
    			do{
    				StringTokenizer colon = new StringTokenizer(sc.nextLine(), ":");
    				voter = new ArrayList<String>();
    				while(colon.hasMoreTokens()){
    					voter.add(colon.nextToken());
    				}
    				voterMaster.add(voter);
    			}
    			while(sc.hasNextLine());
    		} 
    		
    		catch (FileNotFoundException e) {
    			JOptionPane.showMessageDialog(null, "ERROR: VOTER FILE DOES NOT EXIST.\nLaunch again with correct file.", "ERROR", JOptionPane.ERROR_MESSAGE);
    			System.exit(0);
    			e.printStackTrace();
    		}
    	}
    
    	public boolean checkUserExistence(String user) {
    		
    		for(int i = 0; i < voterMaster.size(); i++){ //ERROR IS IN THIS LINE
    			System.out.println(voterMaster.get(i).get(1));
    			if(voterMaster.get(i).get(0).equals(user)){
    				indexOfVoter = i;
    				return true;
    			}
    		}
    		return false;
    	}
    }

  2. #2
    ryanonnfire54 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Here is the trace:



    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at LogOn.checkUserExistence(LogOn.java:44)
    at Voting$MyListener.actionPerformed(Voting.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6348)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:611 3)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4714)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:45 44)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:45 44)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 635)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  3. #3
    ryanonnfire54 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Ugh, nevermind. Created the ArrayList within the constructor, which was not able to be accessed by the checkUserExistence. Sorry >.> Feel free to delete this thread.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Actually, leaving it open is ideal. Then people with the same issue in the future can solve their problem more easily.

    Thanks for sharing the solution!

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    In the try block you define a new votermaster by

    Java Code:
    ArrayList<ArrayList<String>> voterMaster = new ArrayList<ArrayList<String>>();
    This new voterMaster has only scope within the block statement and hides the voterMaster of your class.

    I guess if you rewrite the previous statement to:

    Java Code:
    voterMaster = new ArrayList<ArrayList<String>>();
    your problem disappears.

    Erik

    Hm, I'm too late I guess...
    Last edited by venerik; 11-16-2010 at 06:25 PM. Reason: OP already found the solution
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 02:31 PM
  2. Unknown error in Jtable...
    By blu_fox88 in forum AWT / Swing
    Replies: 2
    Last Post: 02-23-2010, 05:48 AM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. NullPointerException at FindAppletJDKLevel(unknown source)
    By PeteMansfield in forum Java Applets
    Replies: 18
    Last Post: 09-21-2009, 06:54 PM
  5. Hi , Error in Jsp page is as "NumberFormatException.forInputString(Unknown Source)"
    By lavanya82 in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-20-2009, 07:22 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
  •