-
January 9th, 2015, 06:26 AM
#1
Beginner's problems with generics
Code:
public static <T> T[] fill(T[] a, Generator<T> gen) {
for(int i = 0; i < a.length; i++)
a[i] = gen.next();
return a;
}
What does the bolded symbol mean?
-
January 9th, 2015, 07:26 AM
#2
Re: Beginner's problems with generics
And why do I get error at runtime for this program:
Code:
import java.util.*
public class BasicHolder<T> {
T element;
void set(T arg) { element = arg; }
T get() { return element; }
void f() {
System.out.println(element.getClass().getSimpleName());
}
public static void main(String[] args){
BasicHolder<String> s1 = new BasicHolder<String>();
s1.f();
}
}
the error is:
Code:
Error: Main method not found in class BasicHolder, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Last edited by flex567; January 12th, 2015 at 09:00 AM.
-
January 12th, 2015, 07:04 AM
#3
Re: Beginner's problems with generics
The posted code does not compile without errors. Fix the compiler errors before trying to execute the code.
Norm
-
January 12th, 2015, 09:01 AM
#4
Re: Beginner's problems with generics
I fixed it now I get
Code:
Exception in thread "main" java.lang.NullPointerException
at BasicHolder.f(BasicHolder.java:8)
at BasicHolder.main(BasicHolder.java:12)
-
January 12th, 2015, 10:02 AM
#5
Re: Beginner's problems with generics
Exception in thread "main" java.lang.NullPointerException
at BasicHolder.f(BasicHolder.java:8)
at BasicHolder.main(BasicHolder.java:12)
There is a variable on line 8 that has a null value when that statement is executed. Look at that line, find the variable with the null value and then backtrack in the code to see why it does not have a valid value.
Norm
-
January 12th, 2015, 10:13 AM
#6
Re: Beginner's problems with generics
I dont know, the entire line = 0?
Code:
System.out.println(element.getClass().getSimpleName());
or class String doesn't have getClass() method
Last edited by flex567; January 12th, 2015 at 10:19 AM.
-
January 12th, 2015, 10:50 AM
#7
Re: Beginner's problems with generics
What does that mean?
There are two values on that line to test for null: element and the value returned by getClass()
Print each of them separately.
Norm
-
January 12th, 2015, 11:03 AM
#8
Re: Beginner's problems with generics
I thought that println() can return 0, but even if it did, the compiler wouldn't throw an error?
now it runs as expected:
Code:
import java.util.*;
public class BasicHolder<T> {
T element;
void set(T arg) { element = arg; }
T get() { return element; }
void f() {
System.out.println(element.getClass().getSimpleName());
}
public static void main(String[] args){
BasicHolder<String> s1 = new BasicHolder<String>();
s1.element = new String();
s1.f();
}
}
thanx
getClass() is not method inside a String class but it is inherited from Object?
I thought I could have problems with the compiler because the name of the class is BasicHolder<T> and the name of the file is just BasicHolder.java
Last edited by flex567; January 12th, 2015 at 11:18 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|