Iterator NPE in java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm having an issue with multiple NullPointerExceptions, I'm getting one for my if statements within iterators and I can't figure out why
one of the problem blocks:
public int removeAllBooks(String author, String title){
Iterator<Book> itr=library.iterator();
int i=0;
while(itr.hasNext()){
Book book=itr.next();
if(b.getAuthor().equals(author)&&(b.getTitle().equals(title))){
itr.remove();
i++;
}
if(i>0){
return i;
}
}
return 0;
}
The NPE error points to my if-statement line for some reason.
Thanks.

What is b? Where is it declared/initialized? Your itr.next() is assigned to the book variable, not b.
You code should probably be :
Book book=itr.next();
if(book.getAuthor().equals(author)&&(book.getTitle().equals(title))){

Related

Tried Java reflection's ".getDeclaredField" method but still meet "NoSuchFieldException" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying modification of an instance's fields through Java reflection.
Here is my original code.
for(Field f: customerClass.getDeclaredFields()) {
System.out.println(f);
}
System.out.println("\n");
System.out.println(customerClass.getDeclaredField("firstName"));
for(int i=0; i < columnNames.length; i++) {
System.out.println(columnNames[i]);
field = customerClass.getDeclaredField(columnNames[i]);
field.setAccessible(true);
And the results.
private java.lang.String Customer.firstName
private java.lang.String Customer.lastName
private java.lang.String Customer.firstName
"firstName"
java.lang.NoSuchFieldException: "firstName"
at java.lang.Class.getDeclaredField(Class.java:2070)
I am wondering why "customerClass.getDeclaredField("firstName")" works, but "customerClass.getDeclaredField(columnNames[i])" throws an Exception, since columnNames[0] == "firstName".
If you'll look at your output for columnNames[0], you'll see that it's not firstName, it's "firstName". Remove the quotes and it should work.

How to make a summ Method in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
recently my teacher showed us how to make a summ methode but i cant remember it.I think i got it a bit ritght.But it tells me error unexpected token a
void main(){
summ(6, 7);
}
int summ(a, b){
a = int
b = int
return a+b }
It should be like this
static int summ(int a, int b)
{
return a+b;
}

JAVA error finding prime no. between 1 to 20 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm new and would like to find and correct my errors. I got 2 errors while writing a java program to print prime nos. between 1 to 20.
code:
error:
; instead of ,
for (i=1;i<20;i++)

missing return statement even method is having void return type [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
am writing a java program as below.
Void fun(int n){
for(int i=0;i<=n;i++){
fun(n-i);
}
System.out.println(“well done”);
}
I am getting error, missing return statement. I not used int or string method. It is void method na. why it asking return type, please help for this problem.
Void is a reference type.
void is a language primitive.
You don't need a return statement when your return type is void.

Can't create two int in for statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Can you please help me make the following code work?
for(int a=0, b=0; a<101; b<102; a++; b++;) {
stuff
}
You got the initialization (first) part of the loop right.
The termination or condition (second) part of the loop should be evaluated to a boolean, so assuming you require an AND relation between the conditions on a and b, it becomes a<101 && b<102. You might want || (OR) instead, depending on your logic.
The increment (third) part of the loop should contain comma separated expressions (same as the initialization part which you already got right).
I also removed an extra ';' from the end.
for(int a=0, b=0; a<101 && b<102; a++, b++) { stuff }

Categories