1.11M Members

non-static method cannot be referenced from a static context

 
-1
 

Hi, i am a newbie at this and i am having problems with the following errors.

U:\year 3\AssignmentFinal\Customer2.java:29: non-static method setName(java.lang.String) cannot be referenced from a static context

i have inserted my code below any help would be greatly appreciated.

// customer class

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
 
public class Customer2 {
	
	private String name;
	private String address;
	private String customerNumber;
	private String emailAddress;
	private String password;
 

	public static void main(String[] args) {
 
		List<Customer> list = new ArrayList<Customer>();
		File file = new File("Customers.txt");
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = null;
 
			while ((line = in.readLine()) != null) {
				String[] record = line.split(",");
				Customer customer = new Customer();
				Customer2.setName(record[0]);
				Customer2.setAddress(record[1]);
				Customer2.setCustomerNumber(record[2]);
				Customer2.setEmailAddress(record[3]);
				Customer2.setPassword(record[4]);
				list.add(customer);
			}
 
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		}
 
 
		//for(Customer2 c : list){
	//		System.out.println(c.getName());
	//	}
		
		
	}


	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCustomerNumber() {
		return customerNumber;
	}
	
	public void setCustomerNumber(String customerNumber) {
		this.customerNumber = customerNumber;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	

}
 
0
 

If you want to call a method like that : Customer2.setName it should be static.

In your case it is correct to have them NON-static; but you create a
"Customer" objetct: Customer customer = new Customer(); and them call the "Customer2" ??
What are you trying to do??

Shouldn't you be doing this:

List<Customer2> list = new ArrayList<Customer2>();


Customer2 customer = new Customer2();
customer.setName(record[0]);

Notice I use the "customer" instance in order to call the non-static method.


If you already have a "Customer" class with the right attributes (name, address, ...) then use that class and do not declare them again at Customer2

 
0
 

Hi, i am a newbie at this and i am having problems with the following errors.

U:\year 3\AssignmentFinal\Customer2.java:29: non-static method setName(java.lang.String) cannot be referenced from a static context

i have inserted my code below any help would be greatly appreciated.

// customer class

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
 
public class Customer2 {
	
	private String name;
	private String address;
	private String customerNumber;
	private String emailAddress;
	private String password;
 

	public static void main(String[] args) {
 
		List<Customer> list = new ArrayList<Customer>();
		File file = new File("Customers.txt");
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = null;
 
			while ((line = in.readLine()) != null) {
				String[] record = line.split(",");
				Customer customer = new Customer();
				Customer2.setName(record[0]);
				Customer2.setAddress(record[1]);
				Customer2.setCustomerNumber(record[2]);
				Customer2.setEmailAddress(record[3]);
				Customer2.setPassword(record[4]);
				list.add(customer);
			}
 
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		}
 
 
		//for(Customer2 c : list){
	//		System.out.println(c.getName());
	//	}
		
		
	}


	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCustomerNumber() {
		return customerNumber;
	}
	
	public void setCustomerNumber(String customerNumber) {
		this.customerNumber = customerNumber;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	

}

This is as simple as that. Main is a static block from where you are accessing the non static setters and getters. That throws the error. Rule to be noted tat you cannot access non-static block from static context, but vice - versa is of course possible. :)

 
0
 

Hi Thanks for the quick reply

I am trying to read the text file and save each line from it into an object so i can read them later. I have made the following changes to my code but now get the error: U:\year 3\AssignmentFinal\Customer2.java:30: cannot find symbol

// customer class

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
 
public class Customer2 {
	
	private String name;
	private String address;
	private String customerNumber;
	private String emailAddress;
	private String password;
 

	public static void main(String[] args) {
 
		List<Customer> list = new ArrayList<Customer>();
		File file = new File("Customers.txt");
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = null;
 
			while ((line = in.readLine()) != null) {
				String[] record = line.split(",");
				Customer customer = new Customer();
				Customer.setName(record[0]);
				Customer.setAddress(record[1]);
				Customer.setCustomerNumber(record[2]);
				Customer.setEmailAddress(record[3]);
				Customer.setPassword(record[4]);
				list.add(customer);
			}
 
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		}
 
 
		//for(Customer2 c : list){
	//		System.out.println(c.getName());
	//	}
		
		
	}


	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCustomerNumber() {
		return customerNumber;
	}
	
	public void setCustomerNumber(String customerNumber) {
		this.customerNumber = customerNumber;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	

}

Thanks for your patients, i am very new to this and i am stuggling to get my head around it.

 
0
 

Could you please tell me where is the Customer class of which the list is made?

 
0
 

Hi i got around the problem following your solutions but now get the following run time error: ArrayIndexOutOfBoundsException1.

// customer class

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
 
public class Customer2 {
	
	private String name;
	private String address;
	private String customerNumber;
	private String emailAddress;
	private String password;
 

	public static void main(String[] args) {
 
		List<Customer2> list = new ArrayList<Customer2>();
		File file = new File("Customers.txt");
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = null;
 
			while ((line = in.readLine()) != null) {
				String[] record = line.split(",");
				Customer2 customer = new Customer2();
				customer.setName(record[0]);
				customer.setAddress(record[1]);
				customer.setCustomerNumber(record[2]);
				customer.setEmailAddress(record[3]);
				customer.setPassword(record[4]);
				list.add(customer);
			}
 
		} 
		
		catch (Exception e) {
			e.printStackTrace();
		}
 
 
		for(Customer2 c : list){
			System.out.println(c.getName());
		}
		
		
	}


	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCustomerNumber() {
		return customerNumber;
	}
	
	public void setCustomerNumber(String customerNumber) {
		this.customerNumber = customerNumber;
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	

}

Thanks!

 
0
 

this bcoz the comma separated values should be 5. as you have given the record[] till record[5]. Try giving more names in customers text file atleast 5 by comma separated and check the result.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article