Exception in thread "main" java.lang.NullPointerException
this is my code....i have error wen compile.
error message : Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:47)
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String bkId = null;
String bkTitle = null;
String bkPublisher = null;
String bkAuthor = null;
String mgId = null;
String mgTitle = null;
String mgPublisher = null;
String mgDate1 = null;
String npId = null;
String npTitle = null;
String npPublisher = null;
String npDate2 = null;
int booksAdd;
System.out.println("<><><><><>Welcome to KDU UC Library Management System<><><><><>");
System.out.println("\n1.Add Books \n2.Add Megazine \n3.Add Newspaper");
booksAdd = input.nextInt();
String temp0 = input.nextLine();
switch(booksAdd){
case 1:
Book[] addBooks = new Book[20];
for(int i=0; i<10; i++){
addBooks[i] = new Book();
}
for(int i=0; i<10; i++){
System.out.println("Enter ID:");
bkId = input.nextLine();
for(int j=0;j<addBooks.length;j++){
while(addBooks[j].id.equals(bkId));{
System.out.println("Duplicate ID");
System.out.println("Enter ID for books "+(i+1) + ":");
bkId = input.nextLine();
}
}
System.out.println("Enter Title:");
bkTitle = input.nextLine();
System.out.println("Enter Publisher:");
bkPublisher = input.nextLine();
System.out.println("Enter Author:");
bkAuthor = input.nextLine();
addBooks[i] = new Book(bkId,bkTitle,bkPublisher,bkAuthor);
}
case 2:
Megazine[] addMegazine = new Megazine[20];
for(int i=0; i<10; i++){
addMegazine[i] = new Megazine();
}
for(int i=0; i<10; i++){
System.out.println("Enter ID:");
mgId = input.nextLine();
for(int j=0;j<addMegazine.length;j++){
while(addMegazine[j].id.equals(mgId)){
System.out.println("Duplicate ID");
System.out.println("Enter ID for megazine "+(i+1) + ":");
mgId = input.nextLine();
}
}
System.out.println("Enter Title:");
mgTitle = input.nextLine();
System.out.println("Enter Publisher:");
mgPublisher = input.nextLine();
System.out.println("Enter Date:");
mgDate1 = input.nextLine();
addMegazine[i] = new Megazine(mgId,mgTitle,mgPublisher,mgDate1);
}
case 3:
Newspaper[] addNews = new Newspaper[20];
for(int i=0; i<10; i++){
addNews[i] = new Newspaper();
}
for(int i=0; i<10; i++){
System.out.println("Enter ID:");
npId = input.nextLine();
for(int j=0;j<addNews.length;j++){
while (addNews[j].id.equals(npId)){
System.out.println("Duplicate ID");
System.out.println("Enter ID for newspaper "+(i+1) + ":");
npId = input.nextLine();
}
}
System.out.println("Enter Title:");
npTitle = input.nextLine();
System.out.println("Enter Publisher:");
npPublisher = input.nextLine();
System.out.println("Enter Date:");
npDate2 = input.nextLine();
addNews[i] = new Newspaper(npId,npTitle,npPublisher,npDate2);
}
default:
System.out.println("Invalid");
break;
}
}
}
ps.reply asap
tq
Re: Exception in thread "main" java.lang.NullPointerException
Please use [code] tags [/code] when posting code.
Unformatted code is hard to follow.
And when you do that you can also point out which line is causing the problem.
Re: Exception in thread "main" java.lang.NullPointerException
wat u mean by code tags??
the error is afta i choose the option....den i enter id...
afta the id i gt tat error.
Re: Exception in thread "main" java.lang.NullPointerException
Code:
Your code goes in here all nice and formatted
That there is wrapped in [code] tags [/code].
You still haven't highlighted the line it occurs on.
The stack trace gave you a line number.
And please don't use txt spk.
This forum does not charge by the letter...
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
Originally Posted by
rougeking
obviously it throws NullPointerException.......
while using input.nextLine() JVM is expecting some input from us.....
what if there is no input......hence it throws NullPointerException...
No it doesn't.
nextLine() will not throw an NPE.
In addition, the stack trace is quite clear it is not being thrown by that method.
Finally, nextLine() will block until data is available, so if there is no input it will sit there waiting.
Quote:
Originally Posted by
rougeking
we have to handle that exception......
i prefer using try-catch block around each input.nextLine() line...
else u can use "throws NullPointerException" beside main......
hope its useful :)
You should almost never catch an NPE like that, as it is almost always a sign that there is something broken in your code.
Also there is no need to "throws NullPointerException" as it is a RuntimeException.
Honestly, if you do not know how Java works it might be an idea for you not to answer the question.
Re: Exception in thread "main" java.lang.NullPointerException
can u did it for me ???
with the try catch block
Re: Exception in thread "main" java.lang.NullPointerException
Did you read what I wrote?
rougeking is wrong.
You do not need a try/catch.
You need to identify what on line 47 is null and then figure out why it is null.
We can help on that, but not if you continue to ignore what is asked of you.
Re: Exception in thread "main" java.lang.NullPointerException
can u told me what i should replace in line 47 ???
can u help me
Re: Exception in thread "main" java.lang.NullPointerException
I have no idea what line up there is line 47.
I can't even follow the code as I cannot read it since it is not wrapped in code tags.
You must know what line 47 is...which is why I keep asking you to highlight it.
Re: Exception in thread "main" java.lang.NullPointerException
am i being an idiot here or has the Book[] class not been implemented into the class?
so wouldn't it fail to compile as class test has no idea what a Book[] addBooks = new Book[20]; is?
Re: Exception in thread "main" java.lang.NullPointerException
rougeking's advice is bad enough that I have deleted his post.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
Originally Posted by
monkeyjr97
am i being an idiot here or has the Book[] class not been implemented into the class?
so wouldn't it fail to compile as class test has no idea what a Book[] addBooks = new Book[20]; is?
Don't ask me, I tend not to read large chunks of unformatted code.