Results 1 to 12 of 12
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default static method within same package not read

    :confused:

    Java Code:
    [B]PairTestOne.java[/B]
    package homenetwork.bkr.training;
    
    public class PairTestOne {
    
    	public static void main (String[] args)
    	{
    		String[] words = {"Mary", "had", "a", "little", "lamb"};
    		[B]Pair<String> mm = ArrayAlg.minmax(words);[/B]
    		System.out.println("min = " + mm.getFirst());
    		System.out.println("max = " + mm.getSecond());
    	}
    }
    
    
    [B]ArrayAlg.java[/B]
    package homenetwork.bkr.training;
    /** Gets the minimum and maximum of an array of strings.
     * @param a: an array of strings
     * @return a: pair with the min and max value, or null if a is null or empty
     */
    public class ArrayAlg {
    		public static Pair<String> minmax(String[] a)
    		{
    			if (a == null || a.length == 0) return null;
    			String min = a[0];
    			String max = a[0];
    			for (int i = 1; i < a.length; i++)
    			{
    				if (min.compareTo(a[i] > 0) min = a[i]);
    				if (min.compareTo(a[0] < 0) max = a[i]);
    			}
    			return new Pair <String>(min, max);
    		}
    }
    Errors:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Pair cannot be resolved to a type
    The method minmax(String[]) from the type ArrayAlg refers to the missing type Pair

    at homenetwork.bkr.training.PairTestOne.main(PairTest One.java:8)

  2. #2
    einarb is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    It connot find the Pair class, so you probably need to import it in both files(or compile/create/put it on the classpath)

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote Originally Posted by einarb View Post
    It connot find the Pair class, so you probably need to import it in both files(or compile/create/put it on the classpath)
    I'm running the program within Eclipse, and so far never had to import classes within the same package..bug comes to mind :)
    Last edited by Fubarable; 06-11-2009 at 12:34 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    I'm running the program within Eclipse, and so far never had to import classes within the same package..bug comes to mind :)
    To be honest, it's not a bug if it doesn't even compile. And this doesn't compile because it cannot find the Pair class. Either you need to write a Pair class, or you need to find where the Pair class is and import it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    But it is a bug -- a bug in the Original poster's code. To Jon: I hope you weren't thinking that this is a Java or Eclipse bug, right? The odds of your current problem being due to a Java bug or Eclipse bug vs a problem with your interpretation of how Java works is astronomically small. ... and I'm not putting you down here. This same statement would apply to bugs in my code too.
    Last edited by Fubarable; 06-11-2009 at 02:48 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    But it is a bug -- a bug in the Original poster's code. To Jon: I hope you weren't thinking that this is a Java or Eclipse bug, right? The odds of your current problem being due to a Java bug or Eclipse bug vs a problem with your interpretation of how Java works is astronomically small. ... and I'm not putting you down here. This same statement would apply to bugs in my code too.
    I tend to reserve the word "bug" for code that actually compiles but doesn't work as it should. Prior to that they're compilation errors, not bugs. I don't think I've ever seen a bug report for a compilation error, for example.
    :)

  7. #7
    Intelrate's Avatar
    Intelrate is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Bug or not a bug

    I saw a bug in our Jira for compilation error when code compiles in Eclipse but Ant target doesn't work ;)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by Intelrate View Post
    I saw a bug in our Jira for compilation error when code compiles in Eclipse but Ant target doesn't work ;)
    Bah!
    There's always one!
    :)

    ETA: Ah! That's a bug in the ant script...:D
    Last edited by Tolls; 06-11-2009 at 06:08 PM.

  9. #9
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I understand that the interpretation of a bug has a pre-condition that the code must compile. I also understand that a bug has been mentioned that compiles in Eclipse but does not compile in Ant.

    It would be nice if you were to share the details of such bug, just in case I happen to encounter it.

    Unfortunately the original author of the code, who is not the same person posting the code is probably not contactable; if it were my code I'd probably have a better idea on how to troubleshoot it.

    Any idea where my code is buggy and what I would need to do to fix it?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    I understand that the interpretation of a bug has a pre-condition that the code must compile. I also understand that a bug has been mentioned that compiles in Eclipse but does not compile in Ant.

    It would be nice if you were to share the details of such bug, just in case I happen to encounter it.

    Unfortunately the original author of the code, who is not the same person posting the code is probably not contactable; if it were my code I'd probably have a better idea on how to troubleshoot it.

    Any idea where my code is buggy and what I would need to do to fix it?
    Sorry about the derail...

    The code is expecting a class Pair. Looking at it it's a generic, since it's the generic thingy <class>.

    Edit: Really must learn to read better...missed that ArrayAlg is posted in the OP as well!

    OK, so somewhere you need the class Pair. That's what's missing. Is it anywhere in the project?

  11. #11
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Java Code:
    [B]PairTestOne.java[/B]
    package homenetwork.bkr.training;
    
    public class PairTestOne {
    
    	public static void main (String[] args)
    	{
    		String[] words = {"Mary", "had", "a", "little", "lamb"};
    		Pair<String> mm = Pair.minmax(words);
    		System.out.println("min = " + mm.getFirst());
    		System.out.println("max = " + mm.getSecond());
    	}
    }
    
    [B]Pair.java[/B]
    package homenetwork.bkr.training;
    /** Gets the minimum and maximum of an array of strings.
     * @param a: an array of strings
     * @return a: pair with the min and max value, or null if a is null or empty
     */
    public class Pair {
    		public static Pair<String> minmax(String[] a)
    		{
    			if (a == null || a.length == 0) return null;
    			String min = a[0];
    			String max = a[0];
    			for (int i = 1; i < a.length; i++)
    			{
    				if (min.compareTo(a[i] > 0) min = a[i]);
    				if (min.compareTo(a[0] < 0) max = a[i]);
    			}
    			return new Pair <String>(min, max);
    		}
    }
    Error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type Pair is not generic; it cannot be parameterized with arguments <String>
    The method minmax(String[]) is undefined for the type Pair

    at homenetwork.bkr.training.PairTestOne.main(PairTest One.java:8)


    :confused:

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    Error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type Pair is not generic; it cannot be parameterized with arguments <String>
    The method minmax(String[]) is undefined for the type Pair

    at homenetwork.bkr.training.PairTestOne.main(PairTest One.java:8)


    :confused:
    Pretty self explanatory. If you don't know about generics then you'll need to read up on them to write a generic class, which is what you're trying to do with Pair.

    There's a tutorial for it over at the Sun site, but I don't have a link handy.

Similar Threads

  1. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  2. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 05:48 AM
  3. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 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
  •