Reputation: 1010
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class n00686349
{
public static void main(String args [])throws FileNotFoundException
{
String control;
FileInputStream fis = new FileInputStream("C:/Users/Steven Fowler/Desktop/data.txt");
Scanner scanner = new Scanner(fis);
vehicle one = new vehicle();
while(scanner.hasNextLine())
{
control = scanner.nextLine();
if (control.equals("vehicle") == true)
{
new vehicle();
one.setOwner(control);
}
else if (control.equals("car") == true)
{
System.out.println("You doing alright");
}
else if (control.equals("truck") == true)
{
System.out.println("You doing alright");
}
else if (control.equals("bicycle") == true)
{
System.out.println("You doing alright");
}
else if (control.equals("american car") == true)
{
System.out.println("You doing alright");
}
else if (control.equals("foreign car") == true)
{
}
}
scanner.close();
}
public class vehicle
{
private String ownersName;
private String address;
private String phone;
private String email;
public vehicle()
{
this.ownersName = ("none");
this.address = ("none");
this.phone = ("none");
this.email = ("none");
}
public void setOwner (String ownersName)
{
this.ownersName = ownersName;
}
public void setAddress (String address)
{
this.address = address;
}
public void setPhone (String phone)
{
this.phone = phone;
}
public void setEmail (String email)
{
this.email = email;
}
public String ownersName()
{
return ownersName;
}
public String address ()
{
return address;
}
public String phone ()
{
return phone;
}
public String email()
{
return email;
}
}
class car extends vehicle
{
private boolean convertible;
private String color;
public car()
{
convertible = false;
color = ("none");
}
public void setConvertible (boolean convertible)
{
this.convertible = convertible;
}
public void setColor (String color)
{
this.color = color;
}
public boolean convertible ()
{
return convertible;
}
public String color ()
{
return color;
}
}
Above is my code for a class that is reading input from a file line by line and processing the info into an object(Not complete still building). I understand why you would commonly get this error message. You can't access an instance methods without first creating the object, assigning a reference to the object, and then calling the objects methods. This error pops up in line 16 and 23 whenever I try and creating a new object. So i have been fooling around with this a bit and if I change the class vehicle to static it work. So my questions for that are: is changing the class to static changing all the variables static and is that why it works? What are the implications for that?(Making a class static that is creating instances) and is my using this.variable in my vehicle class and then trying to extend that class causing this issue? I'm not terribly familiar with using the this keyword.
Thanks
Upvotes: 0
Views: 880
Reputation: 511
Make you vehicle class static like this :
public static class vehicle
Upvotes: 1
Reputation: 2015
Making the Vehicle inner class static is not a big deal. It just means that the inner class won't have any way to reference things from the outer class, which you're not doing anyway.
Making all of the members of Vehicle static means that all vehicles share one copy of those properties. All vehicles will have the same owner, for instance.
What I would probably do is move everything from the main
method to a new method. (Call it run
or something like that.)
Then change main to be:
public static void main(String[] args) {
n00686349 obj = new n00686349();
obj.run();
}
Upvotes: 1
Reputation: 201537
You don't assign your new vehicle() to your one
reference. Change this
new vehicle();
one.setOwner(control);
to
one = new vehicle();
one.setOwner(control);
Upvotes: 0