CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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?

  2. #2
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  4. #4
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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)

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  6. #6
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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.

  7. #7
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Beginner's problems with generics

    the entire line = 0?
    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

  8. #8
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    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
  •  





Click Here to Expand Forum to Full Width

Featured