Results 1 to 12 of 12
  1. #1
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Exclamation Method cannot be referenced from a static context - HELP

    I have a small code snippet that adds a customer to a database when the add button is clicked. I get the error when I click the button:

    Java Code:
    non-static method addCustomer(Customer) cannot be referenced from a static context
                                 Customer.addCustomer(aCustomer);
                                             ^
    Here are the relevant parts of my code:

    Customer class:

    Java Code:
    public void addCustomer(Customer aCustomer) {	CustomerDA.addCustomer(this);	}
    Function call:

    Java Code:
    Customer aCustomer = new Customer(fname, lname, address, city, state, zipcode, phone);
    			
    			try {
    				aCustomer.addCustomer(aCustomer);
    				JOptionPane.showMessageDialog(this, "Customer Added");
    				clearForm();

    The addCustomer function:

    Java Code:
    public static void addCustomer(Customer aCustomer)
    	{
    		String fname = aCustomer.getfName();
    		String lname = aCustomer.getlName();
    		String address = aCustomer.getAddress();
    		String city = aCustomer.getCity();
    		String state = aCustomer.getState();
    		String zipcode = aCustomer.getZipcode();
    		String phone = aCustomer.getPhone();
    		
    		String query = "INSERT INTO Customer VALUES ('"+"', '" + fname + "', '"+ lname + "', '" + address + "', '"+ city + "', '"+ state + "',  '"+ zipcode + "', '"+ phone + "')";
    		
    		boolean found = find(fname, lname);
    		
    		if (!found) {
    			try {
    				  stmt.executeUpdate(query);
    			}
    			catch (SQLException e) {
    				System.out.println(e);
    		  }
    	  }
      }
    I don't know where the problem is coming from. Please help!! Thanks:D

  2. #2
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    I didn't read the whole code, but I know what that error message means. A non-static method can call a static method, but not vice versa. A non-static method refers to an object and a static method refers to the class itself. So, a static method can't call a method from the object, since it has no idea which object to use.

  3. #3
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    Ok, so I had a dumb mistake but fixed it. I changed

    Java Code:
    Customer.addCustomer(aCustomer);
    to
    Java Code:
    aCustomer.addCustomer(aCustomer);
    Customer is the class, aCustomer is the object created.

    Now I have a null pointer exception when I click the add button to add the customer to the database. It all traces back to this though. Any ideas? Thanks in advance!

  4. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    You're adding the customer aCustomer to itself? That may be what's causing the error, but I can't tell since I don't know the code of the class.

  5. #5
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    I'm a student, new to java so I'm still learning. Here is exactly what I want to do. Fill out a form and click the add button. By clicking the add button, the customer should be entered into the database.

    From the customer class:

    Java Code:
    //CONSTRUCTOR
    public Customer(String newfName, String newlName, String newAddress, String newCity, String newState, String newZipcode, String newPhone) {
    		
    		setfName(newfName);
    		setlName(newlName);
    		setAddress(newAddress);
    		setCity(newCity);
    		setState(newState);
    		setZipcode(newZipcode);
    		setPhone(newPhone);
    	}
    
    
    //ADD FUNCTION
    	public void addCustomer(Customer aCustomer) {	CustomerDA.addCustomer }
    This is from my GUI that adds the customer:

    Java Code:
    private void addNew() {
    		String fname = fnametxt.getText();
    		String lname = lnametxt.getText();
    		String address = addresstxt.getText();
    		String city = citytxt.getText();
    		String state = statetxt.getText();
    		String zipcode = zipcodetxt.getText();
    		String phone = phonetxt.getText();
    		
    		if(fname.length() == 0 || lname.length() == 0 || address.length() == 0 || city.length() == 0 || state.length() == 0 || zipcode.length() == 0 || phone.length() == 0)
    			JOptionPane.showMessageDialog(this, "Please fill in entire form ");
    		else {
    			Customer aCustomer = new Customer(fname, lname, address, city, state, zipcode, phone);
    			
    			try {
    				aCustomer.addCustomer(aCustomer);
    				JOptionPane.showMessageDialog(this, "Customer Added");
    				clearForm();
    			}
    			catch(Exception e) {
    				JOptionPane.showMessageDialog(this, e.toString());
    			}
    		}
    	}
    And finally, the function that actually interacts with the database:

    Java Code:
    public static void addCustomer(Customer aCustomer)
    	{
    		String fname = aCustomer.getfName();
    		String lname = aCustomer.getlName();
    		String address = aCustomer.getAddress();
    		String city = aCustomer.getCity();
    		String state = aCustomer.getState();
    		String zipcode = aCustomer.getZipcode();
    		String phone = aCustomer.getPhone();
    		
    		String query = "INSERT INTO Customer VALUES ('"+"', '" + fname + "', '"+ lname + "', '" + address + "', '"+ city + "', '"+ state + "',  '"+ zipcode + "', '"+ phone + "')";
    		
    		boolean found = find(fname, lname);
    		
    		if (!found) {
    			try {
    				  stmt.executeUpdate(query);
    			}
    			catch (SQLException e) {
    				System.out.println(e);
    		  }
    	  }
      }

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    What's the CustomerDA class?

    Can you provide inline comments in your code so we know what you are trying to accomplish with specific lines?

    Without much information, not much can be said on how to progress, though it seems that you need a better understanding of using classes with one another, especially calling their methods.
    Last edited by emceenugget; 11-18-2008 at 02:49 AM. Reason: wanted to clarify

  7. #7
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    This is application is going to be for a school project, so I am having to follow their guidelines, this is why I have a CustomerDA class (data access class).

    The customer DA class is the only class that actually interacts with the database. The add, delete, find, and a few other methods are in this class. I need to know how to call the add function that is in the CustomerDA class from a different class. I will re-post the code that I posed above with comments.

    Java Code:
    Both of these functions are in the Customer class
    
     //CONSTRUCTOR
    //takes in parameters and uses set functions to set the parameters to private variables in this class
    
    public Customer(String newfName, String newlName, String newAddress, String newCity, String newState, String newZipcode, String newPhone) {
    		
    		setfName(newfName);
    		setlName(newlName);
    		setAddress(newAddress);
    		setCity(newCity);
    		setState(newState);
    		setZipcode(newZipcode);
    		setPhone(newPhone);
    	}
    
    
    //ADD FUNCTION
    //this function is a simple call to the add function in the CustomerDA class.
    	public void addCustomer(Customer aCustomer) {	CustomerDA.addCustomer }
    Java Code:
    //This function is in the GUI class.  When the add button is clicked, this 
    //function is called.
    
    private void addNew() {
    		String fname = fnametxt.getText();
    		String lname = lnametxt.getText();
    		String address = addresstxt.getText();
    		String city = citytxt.getText();
    		String state = statetxt.getText();
    		String zipcode = zipcodetxt.getText();
    		String phone = phonetxt.getText();
    
    //reads in values from the form ^^
    //if anything is blank, display message to fill in entire form.
    		
    		if(fname.length() == 0 || lname.length() == 0 || address.length() == 0 || city.length() == 0 || state.length() == 0 || zipcode.length() == 0 || phone.length() == 0)
    			JOptionPane.showMessageDialog(this, "Please fill in entire form ");
    		else {
    
    //once the form is filled out completely, create a new customer object
    
    			Customer aCustomer = new Customer(fname, lname, address, city, state, zipcode, phone);
    			
    			try {
    //then call the addCustomer function in the Customer class
    //(which then calls the addCustomer function in the CustomerDA class)
    
    				aCustomer.addCustomer(aCustomer);
    				JOptionPane.showMessageDialog(this, "Customer Added");
    				clearForm();
     
     //if customer is added, display confirmation message and clear the form.
    			}
    			catch(Exception e) {
    				JOptionPane.showMessageDialog(this, e.toString());
    			}
    		}
    	}
    And finally, the addCustomer function in the CustomerDA class. This is the one that actually does the work.

    Java Code:
    public static void addCustomer(Customer aCustomer)
    	{
    		String fname = aCustomer.getfName();
    		String lname = aCustomer.getlName();
    		String address = aCustomer.getAddress();
    		String city = aCustomer.getCity();
    		String state = aCustomer.getState();
    		String zipcode = aCustomer.getZipcode();
    		String phone = aCustomer.getPhone();
    
    //gets values from the customer object and inserts the values into a query		
    		String query = "INSERT INTO Customer VALUES ('"+"', '" + fname + "', '"+ lname + "', '" + address + "', '"+ city + "', '"+ state + "',  '"+ zipcode + "', '"+ phone + "')";
    		
    		boolean found = find(fname, lname);
    
    //if the customer isn't found, execute the query, otherwise the customer is
    //already in the database.
    		
    		if (!found) {
    			try {
    				  stmt.executeUpdate(query);
    			}
    			catch (SQLException e) {
    				System.out.println(e);
    		  }
    	  }
      }

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Do your given specifications require specific input parameters? It seems like you need to just define addCustomer() as a static method, but you probably tried that. Do you know about the static modifier?

    If you're having too much trouble and you have a little freedom on how you can implement things, you can make addCustomer() to simply add the current instance to the database instead of taking an input. I don't see anything wrong with it, since it won't change the user interface.

  9. #9
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    Yeah, I've tried using a static method. When I do that, I always get a null pointer exception and I can't track it down. I have had very limited experience in java and I think I may have taken on a bigger project than I should have.

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    I don't think it's that difficult of a project to accomplish. You just need to get used to it. Most of the work is done by fixing compile time errors. The compiler should tell you where your NullPointerException occurs. From there, it shouldn't be hard to see what you are lacking.

  11. #11
    jmorris is offline Member
    Join Date
    Nov 2008
    Posts
    12
    Rep Power
    0

    Default

    Yeah, its not too bad of a project. There is a lot more going on here too though. Thanks for the help. Oh and by the way, its a run-time error on the null pointer.

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Normal practice is to have a separate class that does the "addCustomer" function. (typically called a CustomerFactory)

    You call code something like:

    Java Code:
    Customer aCust = CustomerFactory.addCustomer();
    aCust.setName("alfred E Neuman);
    aCust.setBirthdate(....);
    aCust.save();
    Your code has the problem that you want to set things in the Customer object and when you are calling "addCustomer" as a static function, you don't have a Customer to work on

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. Static variable context Errors ?
    By Shyam Singh in forum New To Java
    Replies: 16
    Last Post: 08-08-2008, 09:11 PM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •