Results 1 to 12 of 12
- 11-26-2012, 08:40 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 22
- Rep Power
- 0
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
- 11-26-2012, 11:22 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 11-26-2012, 11:37 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 22
- Rep Power
- 0
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.
- 11-26-2012, 12:30 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Exception in thread "main" java.lang.NullPointerException
That there is wrapped in [code] tags [/code].Java Code:Your code goes in here all nice and formatted
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...Please do not ask for code as refusal often offends.
- 11-26-2012, 12:38 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Exception in thread "main" java.lang.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.
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.Please do not ask for code as refusal often offends.
- 11-26-2012, 03:45 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 22
- Rep Power
- 0
Re: Exception in thread "main" java.lang.NullPointerException
can u did it for me ???
with the try catch block
- 11-26-2012, 04:08 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 11-26-2012, 04:49 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 22
- Rep Power
- 0
Re: Exception in thread "main" java.lang.NullPointerException
can u told me what i should replace in line 47 ???
can u help me
- 11-26-2012, 05:01 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 11-27-2012, 03:30 AM #10
Senior Member
- Join Date
- Nov 2012
- Posts
- 228
- Rep Power
- 1
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.
- 11-27-2012, 09:50 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Another Exception in thread "main" java.lang.NullPointerException problem
By wdh321 in forum New To JavaReplies: 6Last Post: 04-19-2012, 07:10 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks