Can't Compile Basic Java Program

EMB01
EMB01 used Ask the Experts™
on
Maybe I'll actually receive some help with this...

When I try to compile the following code I get this error:

public void toString()
return type void is not compatible with string
public class Roster
{
	int[] StudentId;
	String[] StudentName;
	int[] StudentAge;
	
	public void toString()
	{
		for (int i = 0; i < StudentId.length; i++)
		{
			System.out.println("###### Student " + i + " ######");
			System.out.println("ID: " + StudentId[i]);
			System.out.println("Name: " + StudentName[i]);
			System.out.println("Age: " + StudentAge[i]);
		}
	}
	
	public void CreateTestData()
	{
		for (int i = 0; i < 20; i++)
		{
			StudentId[i] = i;
			StudentName[i] = "Name";
			StudentAge[i] = 1;
		}
	}
	
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
toString() is already defined in Object to return a string, and since declaring a method named that in a class basically means that you are overriding Object's toString, you can't override a method with a different return type.

Either change the name of the method, so that you aren't overriding Object's toString(), or make the return type of your one a String, and return something, even null or an empty string.

Commented:
Hi.
Change method name "toString" to something else.

Author

Commented:
Thank you that's helpful.  

I have another problem with this script, though.

When I go to run this (the script that uses the class above) it says:

Exception in thread "main" java.lang.NullPointerException
at Roster.CreateTestData(Roster.java:24)
at Driver.main(Driver.java:10)
public class Driver
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);

		Roster myRoster = new Roster();
		myRoster.CreateTestData();

		myRoster.toString();

	}
}

Open in new window

IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
You have declared array variables (in lines 3,4,5 of original code) but you haven't created the arrays before trying to assign values (at lines 22,23,24). Change the Roster class as below... (to get it to compile and run anyway. However, you would probably still want to do it another way)

public class Roster
{
	int[] StudentId = new int[20];
	String[] StudentName = new String[20];
	int[] StudentAge = new int[20];
	
	public String toString()
	{
		for (int i = 0; i < StudentId.length; i++)
		{
			System.out.println("###### Student " + i + " ######");
			System.out.println("ID: " + StudentId[i]);
			System.out.println("Name: " + StudentName[i]);
			System.out.println("Age: " + StudentAge[i]);
		}
                return "";
	}
	
	public void CreateTestData()
	{
		for (int i = 0; i < 20; i++)
		{
			StudentId[i] = i;
			StudentName[i] = "Name";
			StudentAge[i] = 1;
		}
	}
	
}

Open in new window

Author

Commented:
Thanks, mccarl.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial