-3

So here is my code... It's about asking for house pricing and locations, but I keep getting a NullPointerException.

 public class price {

int[] Hprice;
String [] Hlocation;

int i = 0;

public static void main(String[] args) {

    price price = new price();
    price.input();

}

public void input()
{
    price price = new price();
    Scanner scan = new Scanner(System.in);  

        System.out.println("what is the price of the house?");
        int ansP = scan.nextInt();
        price.Hprice[i]=ansP;
        System.out.println("what is its location? (north,south,east,west)");
        String ansL = scan.nextLine();
        price.Hlocation[i]=ansL;
        i +=1;
}

when i run this ii get a null pointer exception, i have tried to define every value in the array but i stil get the exception when i set Hprice = ansP. Does anyone know where the excpetion might be happening? Thanks!

LucasH
  • 9
  • 1
  • 1
    "Does anyone know where the excpetion might be happening?" The exception tells you where... – tkausl Oct 06 '16 at 02:28
  • 2
    For a start code like `price price = new price();` is very confusing and prone to errors (follow java standards. Seconding the `input` method is part of the `price` class so why are you calling `price price = new price();` ? – Scary Wombat Oct 06 '16 at 02:30
  • Another thing - When you read input normally from Scanner, there is always a `\n` character, essentially marking a new line when the user inputs anything. When you take String input (e.g. `scan.nextLine();`), then this character is picked up by that method, and is not left in the queue of tokens to read from. However, `\n` can't be read as an int, so when you call `scan.nextInt()`, it's always a good idea to do a `scan.nextLine()` afterwards if it is at the end of a line (and not `13 asdf\n` as input). – MercyBeaucou Oct 06 '16 at 04:46

1 Answers1

1

You both arrays never been instantiated and the size wasn't set