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

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.

Related

'Not a statement', '; expected' [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 5 years ago.
Improve this question
I am getting both these errors (on the marked line) with the following code in java;
String data[] = file.getInput();
while(data[0] != "X"){
String ID = data[0];
String firstName = data[1];
String lastName = data[2];
data[] = file.getInput(); //errors occurr here
}
Note that file.getInput() is a method that returns an array of Strings from a CSV file using the InputReader.
Just remove the [] from data.
data = file.getInput();
Remove the ‘[]’ from data :
data = file.getInput(); ‘Do the statement like this’

Difference between String and string [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 6 years ago.
Improve this question
When using Java, what is the difference between the two following assignments:
String upperCaseDataType = "myName";
string lowerCaseDataType = "myName";
Do the 2 mean the same at compile time, just like in C#?
Thanks very much for your help.
string is not a class or type in Java
No. string s = "myName"; is not legal Java, and will not compile. Also, those are assignments (not assertions).

Iterator NPE in java [closed]

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))){

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.

Illegal format string in TextIO.putf() method aka wrong Array 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
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.

Categories