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.