Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Null Pointer exception

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Pointer exception

    Hey guys, I am a noob to java, and I am so confused at this point in my code that I had to come here. I for some reason am getting a nullpointerException when trying to add something to my arraylist from my creative project RPG for AP computer science. I need this to work.

    Also, do you guys know how to use a variable between methods, like I wanted to be able to set direction to something and have it used with that "something" in another method, I doubt this is possible but it was worth asking, anyway here is the code.
    import java.util.*;
     
    public class rpgScript {
    	int i;
      	int j;
     
     
      	String move = "";
    	public ArrayList<String> ch;
    	public ArrayList<String> ccch;
    	public int index;
    	ArrayList<String> inventory;
    	public int MAXIMUM_SIZE;
    	public String choice;
    	public int weapon;
    	public int counter; 
    	public String direction;
     
     
     	public rpgScript() {
     
    	    rpgInput Input = new rpgInput(ch);
    	    ArrayList<String> inventory = new ArrayList<String>();
    		int counter= 0;
    		int weapon = 0;
    		int inv=0;
    		int index=0;
    		int MAXIUMUM_SIZE = 0;
    		String direction=" ";
    	    String choice="";
    	    Scanner reader = new Scanner(System.in);
    	    ch = ccch;
     
    	}
    	public void f1() throws InterruptedException
    		{
     
    		MainRPG main = new MainRPG(ch);
    		ArrayList<String> inventory = new ArrayList<String>();
    		Scanner reader = new Scanner(System.in);
    		System.out.println("Ah, you started out in the forest, very well...");
    		Thread.sleep(2000);
    		System.out.println("");
    		System.out.println("You are now in the forest, there is a sword, piece of bread,");
    		System.out.println("");
    		System.out.println("and some shoes on the ground, choose one. Or choose none at all.");
    		choice = reader.nextLine();
    		[B]addAtSlot(0,choice);[/B]
    		//if(choice.equals("north")||choice.equals("south")||choice.equals("west") || choice.equals("east"))
    	//	{
    		//	direction = choice;
    	//	}
    	//	if(!(choice.equals("north")||choice.equals("south")||choice.equals("west") || choice.equals("east")))
    //	//	addAtSlot(index,choice);
    //		if(!(choice.equals(direction)))
    			index++;
    		}
    	public void f2()
    	{
    		System.out.println("You go " + direction + "You start going deeper into the forest");
    	}
     
    	public void f3()
    	{
    		System.out.println("There are lizards here, if you proceed you may encounter a battle");
    		System.out.println("However, if you did not pick up a good weapon you may not be able to kill it");
    	}
     
    	public void f4()
    	{
    		System.out.println("The lizards attack! I hope you have a weapon!" );
    	}
     
     
     
     
     
     
    	public void f5() 
    	{
    		System.out.println("You decided to start in the city, either that or you somehow made it all the way here");
    		System.out.println("Anyway, you are here now, now venture off if you have not done anything ");
    	}
     
     
     
     
     
     
     
    	 public void grid(ArrayList<String> ch) throws InterruptedException
      {
     
     
      //MainRPG main = new MainRPG(charac);
      rpgScript Scrip = new rpgScript();
      saveRPG save = new saveRPG(ccch);
       rpgScript[][] board = new rpgScript[10][10];
     
     
      	Scanner reader = new Scanner(System.in);
      	String move;
      	String inputFileName;
       System.out.println("Load character: ");
       inputFileName=reader.nextLine();
     
     
     
       for(int i1=0;i1<=9;i1++)
       	for(int j1=0;j1<=9;j1++)
       	{
       	  board[i1][j1] = new rpgScript();
       	}
       if((save.loadModel(ch,inputFileName).get(3)).equals("Forest"));
     
     [B]  board[0][0].f1();[/B]
     
     
     
     
     
     
    	if(i==1 && j==0)
    	board[1][0].f2();
      }	
    //	
     
     
     
    		 public void addAtSlot(int index, String item)
    			{
     
    			[B]if (index < 0 || index >= inventory.size())[/B]
    				{		
    						System.out.println("Cannot add item!");
    						return;
    					}
     
    			if (inventory.get(index) == null)
    					{		
    						System.out.println("There is an item already here!");
    						return;
    					}
     
    				if (inventory.size() == MAXIMUM_SIZE)
    					{
    						System.out.println("Your pack is full!");
    						return;
    					}
    			inventory.add(index, item);
     
    			}
     
    				public void add(String item)
    					{
    						if (inventory.size() == MAXIMUM_SIZE)
    					{
    						System.out.println("Your pack is full!");
    						return;
    					}
    						inventory.add(item);
    					}
     
    			public void remove(int index)
    				{
    					if (index < 0 || index >= inventory.size())
    						{
    							System.out.println("Impossible!");
    							return;
    						}
     
    					if (inventory.get(index) == null)
    						{
    							System.out.println("Cannot remove an item that isn't there!");
    							return;
    						}
    						inventory.remove(index);
    				}
    	}

    Here is the output: --------------------Configuration: <Default>--------------------
    Choose Load or New(for new game)
    load
    Enter a file name to load:
    Demetrius
    2All done, return to the main menu or continue to game? (1) (2)
    2
    Game Start
    Load character:
    Demetrius
    Ah, you started out in the forest, very well...

    You are now in the forest, there is a sword, piece of bread,

    and some shoes on the ground, choose one. Or choose none at all.
    sword
    Exception in thread "main" java.lang.NullPointerException
    at rpgScript.addAtSlot(rpgScript.java:175)
    at rpgScript.f1(rpgScript.java:48)
    at rpgScript.grid(rpgScript.java:116)
    at DriverRPG.main(DriverRPG.java:89)

    Process completed.
    Last edited by Demetrius82; June 2nd, 2011 at 05:30 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Null Pointer exception

    at rpgScript.addAtSlot(rpgScript.java:175)
    look at line 175 in rpgScript. What variable on that line has a null value(ie has not been assigned a value)?
    If you can't tell by looking, add a println just before line 175 to print out all of the variables used on line 175. The print out will show what variable needs a value.
    Now back track in your code to see where you missed giving it a value and fix it

    how to use a variable between methods,
    Here is One way: define the variable outside the methods so they both can see

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer exception

    I did what you said, neither of those values are null, I set index to 0, and it printed 0, and item to "sword" and it printed sword. So the values I put in there are not null. Something weird did occur though, when I tried to find the arraylist size by using this inventory.size() it does not print anything, like literally anything. I am not sure the problem, can you help me with this one?

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer exception

    When I said I wanted to use the variables between methods I meant having a direction that the user inputs and then inside another method take that direction in order to know what to increase and decrease. My code shows what I am trying to do.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Null Pointer exception

    can you help me with this one
    Only if I see the code.

    My code shows what I am trying to do.
    Where is this problem? Can you mark the spot where you are having the problem?

Similar Threads

  1. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  2. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 05:46 PM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM