Null Pointer Exception using Object Arrays -


i planning make airline system. have initialized array using initseats still throws npe error. happens when call seatchecker() bookmenu.

public void initseats(){     for(int b = 0; b < seatlist.length; b++)     {      initc.setname("null");      initc.setemail("null");      initc.setcreditno(0);      initc.setaddress("null");      initc.setpassportno("null");      seatlist[b] = new seat('a', 0, "null", 0.0, "available", initc);        }      for(int d = 0; d <= 24; d++)     {         seatlist[d].setseatletter('a');         seatlist[d].setseatno(d);     }     for(int n = 25; n <= 48; n++)     {         seatlist[n].setseatletter('b');         seatlist[n].setseatno(n);     }     for(int m = 49; m <= 72; m++)     {         seatlist[m].setseatletter('c');         seatlist[m].setseatno(m);     }     for(int t = 73; t <= 96; t++)     {         seatlist[t].setseatletter('d');         seatlist[t].setseatno(t);     }     for(int q = 97; q <= 120; q++)     {         seatlist[q].setseatletter('e');         seatlist[q].setseatno(q);     }     for(int v = 121; v < 144; v++)     {         seatlist[v].setseatletter('f');         seatlist[v].setseatno(v);     }     for(int x = 0; x <= 48; x++)     {         seatlist[x].setsection("front");         seatlist[x].setprice(500);     }     for(int j = 49; j <= 96; j++)     {         seatlist[j].setsection("middle");         seatlist[j].setprice(250);     }     for(int u = 97; u < 144; u++)     {         seatlist[u].setsection("back");         seatlist[u].setprice(100);     } }    public void seatchecker(int index)    {     string status = seatlist[index].getstatus();     if(status.equalsignorecase("available")){             system.out.println("seat available.");         }else{         system.out.println("seat not available. please pick seat.");         bookmenu();         }    }  public void bookmenu() {     int choice1 = 0;     int index;     system.out.println("where want seated?");     system.out.println("[1] front");     system.out.println("[2] middle");     system.out.println("[3] back");     choice1 = sc.nextint();     sc.nextline();     if(choice1 == 1){         system.out.print("choose seat number (0 - 48): ");         index = sc.nextint();         sc.nextline();         seatchecker(index);     }else if(choice1 == 2){         system.out.println("choose seat number (49 - 96): ");         index = sc.nextint();         sc.nextline();         seatchecker(index);     }else if(choice1 == 3){         system.out.println("choose seat number (97 - 144): ");         index = sc.nextint();         sc.nextline();         seatchecker(index);     }else     {         system.out.println("invalid choice. going menu.");         mainmenu();     } } 

null pointer exception code

exception in thread "main" java.lang.nullpointerexception @ pkg.airlines.airlineui.seatchecker(airlineui.java:132) 

seat class

public class seat{ private char seatletter; private int seatno; private string section; private double price; private string status; private customer customerdetails;   public seat(char seatletter, int seatno, string section, double price, string status, customer details)     {     this.seatletter = seatletter;     this.seatno = seatno;     this.section = section;     this.price = price;     this.status = status;     this.customerdetails = details; }  public customer getcustomerdetails() {     return customerdetails; }  public void setcustomerdetails(customer customerdetails) {     this.customerdetails = customerdetails; }  public char getseatletter() {     return seatletter; }  public void setseatletter(char seatletter) {     this.seatletter = seatletter; }  public string getstatus() {     return status; }  public void setstatus(string status) {     this.status = status; }  public double getprice() {     return price; }  public void setprice(double price) {     this.price = price; }  public int getseatno() {     return seatno; }  public void setseatno(int seatno) {     this.seatno = seatno; }  public string getsection() {     return section; }  public void setsection(string section) {     this.section = section; } 

}

probably problems 2 line :

string status = seatlist[index].getstatus(); if(status.equalsignorecase("available")) 

first thing seatlist[index] not initialized . once declare array of references :

seat[] array = new seat[10]; 

the array contains 10 null references seat object . need instantiate them before using them :

seat[0] = new seat(); 

second potential problem be, check :

if(status.equalsignorecase("available")) 

replace :

if("available".equalsignorecase(status)) 

to avoid nullpointerexception in case status null.

p.s. please show seat class understand problem better.


Comments