Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Weird NullPointerException Error

  1. #1
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Question Weird NullPointerException Error

    So I have this code:
    Java Code:
    package com.cjburkey.games.boxee.state;
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.ListSelectionModel;
    import com.cjburkey.games.boxee.Game;
    import com.cjburkey.games.boxee.GamePanel;
    import com.cjburkey.games.boxee.GameState;
    import com.cjburkey.games.boxee.GameStateManager;
    
    public class Menu extends GameState {
    	
    	private String[] options = {"Play", "Help", "Quit"};
    	private int currentSelect = 0;
    	private JPanel levelSelect;
    	public static File[] listOfMaps;
    	@SuppressWarnings("rawtypes")
    	JList list;
    	
    	@SuppressWarnings({ "unchecked", "rawtypes" })
    	public Menu(GameStateManager gsm) {
    		
    		super(gsm);
    		
    		File[] listOfMaps = Game.MapDir.listFiles();
    		List<String> names = new ArrayList<String>();
    		for (int i = 0; i < listOfMaps.length; i++) {
    			if (listOfMaps[i].isFile()) {
    				System.out.println(listOfMaps[i]);
    				InputStream is = null;
    				try {
    					is = new FileInputStream(listOfMaps[i].getPath());
    				} catch (FileNotFoundException e1) {
    					e1.printStackTrace();
    				}
    				BufferedReader br = new BufferedReader(new InputStreamReader(is));
    				try {
    					names.add(br.readLine());
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				levelSelect = new JPanel();
    				levelSelect.setLayout(new FlowLayout());
    				list = new JList(names.toArray());
    				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    				levelSelect.add(list);
    				
    			}
    		}
    		
    	}
    	
    	public void init() { }
    	
    	public void tick() { }
    	
    	public void draw(Graphics g) {
    		
    		g.setColor(new Color(0));
    		g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT);
    		
    		for(int i = 0; i < options.length; i ++) {
    			
    			if(i == currentSelect) {
    				
    				g.setColor(Color.GREEN);
    				
    			} else {
    				
    				g.setColor(Color.WHITE);
    				
    			}
    			
    			g.setFont(new Font("Ariel", Font.PLAIN, 72));
    			g.drawString(options[i], GamePanel.WIDTH / 2 - 75, 150 + i * 150);
    			
    		}
    		
    	}
    	
    	public void keyPressed(int key) {
    		
    		if(key == KeyEvent.VK_DOWN) {
    			
    			currentSelect ++;
    			
    			if(currentSelect >= options.length) {
    				
    				currentSelect = 0;
    				
    			}
    			
    		} else if(key == KeyEvent.VK_UP) {
    			
    			currentSelect --;
    			
    			if(currentSelect < 0) {
    				
    				currentSelect = options.length - 1;
    				
    			}
    			
    		}
    		
    		if(key == KeyEvent.VK_ENTER) {
    			
    			if(currentSelect == 0) {
    				
    				int result = JOptionPane.showConfirmDialog(null, levelSelect, "Select Level", JOptionPane.DEFAULT_OPTION);
    				if(result == JOptionPane.OK_OPTION) {
    					
    					System.out.println(list.getSelectedIndex());
    					//if(list.getSelectedIndex() != -1) {
    						
    						gsm.states.push(new Level1(gsm, listOfMaps[list.getSelectedIndex()].toString()));
    						
    					//}
    					
    				}
    				
    				
    			} else if(currentSelect == 1) {
    				
    				//Help
    				
    			} else if(currentSelect == 2) {
    				
    				System.exit(0);
    				
    			}
    			
    		}
    		
    	}
    	
    	public void keyReleased(int key) {
    		
    		
    		
    	}
    	
    }
    And it returns this error:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at com.cjburkey.games.boxee.state.Menu.keyPressed(Menu.java:130)
    	at com.cjburkey.games.boxee.GameStateManager.keyPressed(GameStateManager.java:33)
    	at com.cjburkey.games.boxee.GamePanel.keyPressed(GamePanel.java:120)
    	at java.awt.Component.processKeyEvent(Component.java:6463)
    	at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
    	at java.awt.Component.processEvent(Component.java:6282)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
    	at java.awt.Component.dispatchEventImpl(Component.java:4731)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:696)
    	at java.awt.EventQueue$4.run(EventQueue.java:694)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    WHY?
    Last edited by cjburkey01; 06-13-2014 at 03:49 AM. Reason: failed at spoilers

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Weird NullPointerException Error

    Hi,

    did you check everything in line 130 for nulls? If the line numers match, then gsm or gdm.state might be null. And same for parameters: list and the element of listOfMaps might be null.

    Just check these values and you should see which value is null.

    Konrad

  3. #3
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Weird NullPointerException Error

    Well, I can't check the value, when I use an if statement, it crashes saying the line with the if statement is a NullPointer

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Weird NullPointerException Error

    What did you do exactly? Can you show us your if statement?

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,096
    Rep Power
    4

    Default Re: Weird NullPointerException Error

    You declare listOfMaps on line 40, making the variable local to the constructor. The field listOfMaps is then still null. Remove the declaration on line 40.
    kneitzel likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,038
    Rep Power
    6

    Default Re: Weird NullPointerException Error

    Why do you do the following?
    Java Code:
    @SuppressWarnings("rawtypes")    JList list;
         
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Menu(GameStateManager gsm) {
    Unless you can document how your code is type safe, you should employ the appropriate
    generic types in your code. JList for one is a genrically types class. So why not declare it as
    such?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Weird NullPointerException Error

    Quote Originally Posted by SurfMan View Post
    You declare listOfMaps on line 40, making the variable local to the constructor. The field listOfMaps is then still null. Remove the declaration on line 40.
    Well seen. This kind of error is hard to spot in my eyes. But chances are quite hight that this was causing the trouble.

    Konrad

  8. #8
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Weird NullPointerException Error

    Ok, I got it working:
    Java Code:
    if(listOfMaps == null) {
        JOptionPane.showMessageDialog(null, "ERROR");
    } else {
        gsm.states.push(new Level1(gsm, listOfMaps[list.getSelectedIndex()].toString()));
    }
    and it DOES return ERROR. so why is it null when I'm assigning a value?

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,096
    Rep Power
    4

    Default Re: Weird NullPointerException Error

    Quote Originally Posted by cjburkey01 View Post
    Ok, I got it working:
    and it DOES return ERROR. so why is it null when I'm assigning a value?
    You did not read my reply at #5.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Cool Re: Weird NullPointerException Error

    Go it!!!!
    replace
    Java Code:
    File[] listOfMaps = blablabla;
    with
    Java Code:
    listOfMaps = blablabla;
    I had already defined it

Similar Threads

  1. Having weird error when compiling
    By Abandoned Mind in forum New To Java
    Replies: 2
    Last Post: 01-10-2014, 06:21 AM
  2. Weird Error With Methods
    By skaterboy987 in forum New To Java
    Replies: 15
    Last Post: 10-17-2011, 04:45 AM
  3. Weird NullPointerException
    By Hollowsoul in forum JCreator
    Replies: 2
    Last Post: 07-13-2011, 11:46 PM
  4. Weird NullPointerException
    By Boreeas in forum New To Java
    Replies: 7
    Last Post: 07-12-2011, 12:30 PM
  5. Weird Error?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03: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
  •