system is printing your first gf was null?

0 khris microbean · September 12, 2014
You could probably tell i'm on the 16th java programming tutorial. I did everything just right(I think) and i'm getting "your first gf was null" instead of what I input.

Post a Reply

Replies

Oldest  Newest  Rating
0 Developer John · October 28, 2014
Yea Eclipse tends to give you errors that stay there until you re-open it.
0 Matthew Abela Medici · October 28, 2014
It still does not work Developer John same problem!

I'm currently doing the Bucky's Java Beginner Tutorials and I'm on the 16th video [titled: "Many Methods & Instances"] if that helps you guys understand with what I'm trying to achieve..
0 Matthew Abela Medici · October 28, 2014
Just to let you guys know that I copied the code for both my main class and second class from Eclipse to NetBeans and the program worked without any errors.. :)
0 Alex Sweps · October 27, 2014
Sorry man, I didnt check the tutorial and just assumed that was your full code :P 
I literally copy and pasted your code into a new project and it worked.... If you using eclipse try go to Project, clean. Remove any imports you dont need aswell. It might be that extra package you have there too. (I didnt copy that part)

@Eugene, yeah kinda rushed through that one...  The setName instantiates it when you call it. 





import java.util.Scanner;

public class Main { //Renamed the main class

public static void main(String[] args){

Scanner input = new Scanner(System.in);
Tuna tunaObject = new Tuna();

System.out.println("Enter the name of your first girlfriend: ");

String temp = input.nextLine();
tunaObject.setName(temp);
tunaObject.saying();

}
}




public class Tuna { //Renamed tuna to Tuna

private String girlName;


public void setName(String name){
girlName = name;
}

public String getName(){
return girlName;
}

public void saying(){
System.out.printf("Your first girlfriend was %s", getName());
}

}
0 Eugene Botma · October 27, 2014
The problem is that your string is never instantiated. I'm not 100% sure about the syntax of java since i haven't used it in quite a while. Try either on of the following:

private String girlName = new String();
//or
private String girlName = ""; //this one is more likely to work.


@alex Sweps, Also you can't call a method directly in a class.
+1 Matthew Abela Medici · October 27, 2014
That is not my main class from which I'm going to output the information.

The other class:

package metjuw;

import java.util.Scanner;

public class apples {

public static void main(String[] args){

Scanner input = new Scanner(System.in);
tuna tunaObject = new tuna();

System.out.println("Enter the name of your first girlfriend: ");

String temp = input.nextLine();
tunaObject.setName(temp);
tunaObject.saying();

}
}


I don't need to insert 
setName("Amanda");


'cause I want the user to input that information.
0 Alex Sweps · October 27, 2014
You havent set girlName to anything. When you use a setter you need to use the method to set the variable thats inside it. 

before you call saying() called the method setName("Amanda"); 


public class tuna {

private String girlName;
public void setName(String name){

girlName = name;
}

public String getName(){

return girlName;

}

setName("Amanda"); //This will put "Amanda" into girlName.

public void saying(){

System.out.printf("Your first girlfriend was %s", getName());
}
}
0 Matthew Abela Medici · October 26, 2014
I'm having an issue with this tutorial aswell, I typed the code for both classes and also re-checked it afterwards with Bucky's code and everything looks in order but I'm getting an error in the "System.out.printf"

public class tuna {

private String girlName;
public void setName(String name){

girlName = name;
}

public String getName(){

return girlName;

}

public void saying(){

System.out.printf("Your first girlfriend was %s", getName());
}
}


The error says:


The method printf (String, Object[]) in the type PrintStream is not applicable for the arguments (String, String).


Anyone knows what's wrong? :/
+1 Developer John · September 12, 2014
Remember, if you want an answer due to a problem with your code, please allow us to see the code. 8-)
+1 Devon Horton · September 12, 2014
If you post what you've completed of it so far that'd make it a lot easier to determine where the issue lies. 
  • 1

Java / Android Development

107,147 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator