Results 1 to 8 of 8
  1. #1
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default The method setName(String) is undefined for the type Person

    Hi I've run into a problem that shouldn't be happening. I'm on the third video tutorial from the eclipse website.

    I have created a simple class called Person:

    Java Code:
    package org.totalbeginner.tutorial;
    
    public class Person {
    	// fields
    	private String name;	// name of person
    	private int maximumBooks;	// the most books a person can check out
    	
    	// constructor
    	public Person()	{
    		name = "unknown name";
    		maximumBooks = 3;
    	}
    	
    	// methods
    	public String getName()	{
    		return name;
    	}
    	
    	public void setName(String anyName) {
    		name = anyName;
    	}
    
    	public int getMaximumBooks() {
    		return maximumBooks;
    	}
    
    	public void setMaximumBooks(int maximumBooks) {
    		this.maximumBooks = maximumBooks;
    	}
    }
    Take note of the setName method, that's where the problem is. I open the scrapbook to learn a bit about that next. I clicked the Import Declarations button and added the org.totalbeginner.tutorial package. I followed the tutorial exactly, that method IS defined in the Person class.

    Into the scrapbook I type:

    Java Code:
    Person p = new Person();
    p.setName("Fred");
    p
    Which leads to the message:

    The method setName(String) is undefined for the type Person
    Why is Eclipse doing this to me?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: The method setName(String) is undefined for the type Person

    What is this scrapbook? Is it an environment for testing some code? Maybe the Person object know by this scrapbook is different to what you have in your Eclipse. Have you tried to recompile it so both of them refer to the same Person class? Because the code looked just fine.

  3. #3
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: The method setName(String) is undefined for the type Person

    I have some more information. For some reason Eclipse seems to think that the Person class is not a part of my current project. I tried to create a new Junit Test Case thingy and calling it PersonTest to test the Person class. However it complained that the Person class wasn't a part of this current project.

    A look at my screen may help:

    The method setName(String) is undefined for the type Person-eclipse.jpg

  4. #4
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: The method setName(String) is undefined for the type Person

    Quote Originally Posted by wsaryada View Post
    What is this scrapbook? Is it an environment for testing some code? Maybe the Person object know by this scrapbook is different to what you have in your Eclipse. Have you tried to recompile it so both of them refer to the same Person class? Because the code looked just fine.
    I've tried to make it point to the same Person class, it should be pointing to it anyway as one step I took was to make sure that doesn't happen (I clicked the Import Declarations button and added the org.totalbeginner.tutorial package.). I tried using "org.totalbeginner.tutorials.Person" but I get the exact same message.

    And yes the Scrapbook is for testing code snippets.

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

    Default Re: The method setName(String) is undefined for the type Person

    That Person class in that screenshot hasn't been saved (it has an '*' next to it), so it hasn't been compiled.
    Not that that is necessarily your problem, but you never know.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: The method setName(String) is undefined for the type Person

    Thanks Tolls, that solved the problem with the scrapbook. Gotta remember things need saving.

    My problem with the Test Case I solved too, take a look at the screenshot I misspelled the highlighted package name to the left :/

    Thanks

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: The method setName(String) is undefined for the type Person

    This thread is more suited to the Eclipse forum than New to Java. Moving.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: The method setName(String) is undefined for the type Person

    Quote Originally Posted by DarrylBurke View Post
    This thread is more suited to the Eclipse forum than New to Java. Moving.

    db
    Sorry about that Darryl, I didn't realize there was an Eclipse forum.

Similar Threads

  1. operator undefined for argument type int, String
    By aconti in forum New To Java
    Replies: 8
    Last Post: 05-30-2011, 08:32 AM
  2. Replies: 10
    Last Post: 12-07-2010, 01:29 PM
  3. Replies: 2
    Last Post: 05-08-2010, 12:02 AM
  4. Replies: 7
    Last Post: 11-03-2009, 03:18 AM
  5. Replies: 0
    Last Post: 05-27-2009, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •