+ 1

Why this program show exception in thread main??? Please help me to check out my error??

here is the code Enter Input like : A B A B M in seperate line https://code.sololearn.com/cCDe7fg6vDHV/?ref=app Dan Walker Donna ‎ ‏‏‎Sreejith  [A^dr3w] Purab Gupta

26th Oct 2018, 3:39 PM
Harsh Agrawal
Harsh Agrawal - avatar
9 Réponses
+ 2
Hi you are getting a null pointer exception if all ticket are not populated. Change the demo method to void demo() throws IOException { Ticket[] tickets= new Ticket[5]; char type='Z'; double rate=0; for(int x=0;x<5;x++) { type= (char)br.read(); if(type=='A') rate=100; else if(type=='B') rate=200; else { type = 'Z'; rate = 0; } br.read(); tickets[x]= new Ticket(type,rate); } for(int x=0;x<5;x++) System.out.println("Ticket "+(x+1)+" "+tickets[x].gettickettype()+" "+tickets[x].getticketrate()); } basically the changes are to the else statement so that any unpopulated tickets are populated with a default value, and the line after the else block just clears the buffer. hope this helps
27th Oct 2018, 11:45 AM
[A^dr3w]
[A^dr3w] - avatar
+ 2
43 just wipes the buffer clean ready for next input import java.io.*; import java.util.Scanner; public class Ticket { char type; double rate; Scanner br= new Scanner(System.in); Ticket() { type= ' '; rate= 0.0d; } Ticket(char Type,double Rate) { type= Type; rate= Rate; } char gettickettype() { return type; } double getticketrate() { return rate; } void demo() throws IOException { Ticket[] tickets= new Ticket[5]; int x; char type=' '; double rate=0; for(x=0;x<5;x++) { try { type = br.next().trim().charAt(0); } catch(Exception e) { type = ' '; rate = 0; } if(type=='A') rate=100; else if(type=='B') rate=200; else { type= ' '; rate= 0; } tickets[x]= new Ticket(type,rate); } for(x=0;x<5;x++) System.out.println("Ticket "+(x+1)+" "+tickets[x].gettickettype()+" "+tickets[x].getticketrate()); } public static void main(String[] args) throws IOException { Ticket obj= new Ticket(); obj.demo(); } }
27th Oct 2018, 2:12 PM
[A^dr3w]
[A^dr3w] - avatar
+ 2
there is no nextChar() in the scanner class in my last post I sent you the code using Scanner
27th Oct 2018, 4:29 PM
[A^dr3w]
[A^dr3w] - avatar
+ 2
when you use Scanner just delete line 43
27th Oct 2018, 4:51 PM
[A^dr3w]
[A^dr3w] - avatar
+ 1
[A^dr3w] Can u tell me what does really 43 line do!!!!! N one more thing when i use Scanner class it is not working can u give me d same code with Scanner class
27th Oct 2018, 1:13 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
[A^dr3w] can der any other process so i did not use 43rd line N one thing also if i use Scanner n take input as this type= sc.nextChar(); //then it wll show error it cant find nextChar method
27th Oct 2018, 4:26 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
[A^dr3w] or whats about 43rd line nd if not any other process den plz xplain me how does it really work??
27th Oct 2018, 4:49 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
[A^dr3w] can any other process so i did not use 45th line //br.read(); and if not den plz xplain 45th line with briefly as i did not use this process anywhere in my code so i did not know how does it really work??? n dont get me wrong for dusturbing u so much and sorry for dat!!!;;
28th Oct 2018, 11:07 AM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
// just makes the line a remark. so //be.read(); was just some residual code that was left in from when I sorted out your code using buffer reader you can just delete the line if you want. if you would like to see what it does run your original code with this line in and then run it without this line to see the difference in the ticket output.
28th Oct 2018, 5:39 PM
[A^dr3w]
[A^dr3w] - avatar