Buenos días,
Tengo un código hecho en Java con errores de NullPointerException. ¿Podéis ayudarme?
El código consiste en 4 clases:
1- Estación, que maneja rutas y agujas (points).
2- Path, es el comienzo de la definición de ruta
3- Route, extiende de path y es la ruta. Tiene dos atributos que son agujas (points)
4- Agujas (points)
Básicamente en el main de Station creo 1 ruta y 2 agujas y asocio esas dos agujas a los atributos de la ruta.
Un saludo y gracias.
================================================== ===
================================================== ===
import java.util.Scanner;
public class Station {
/************ Attributes ****************/
private String stationName;
private int optionInExecution;
private boolean inExecution;
/************ Methods ****************/
private Station ()
{
stationName = "Atocha";
optionInExecution = 0;
inExecution = false;
}
public static void main (String[] args)
{
/* Objects creation */
Station stationAtocha;
stationAtocha = new Station();
Scanner in;
in = new Scanner(System.in);
Route Route1;
Route1 = new Route(); NULLPOINTEREXCEPTION!!!!!!
Point Point1; // Declaration
Point1 = new Point(); // Creation, Instantiation
Route1.point1 = Point1; // Association
Point Point2;
Point2 = new Point();
Route1.point2 = Point2;
System.out.printf(" ============================================\n");
System.out.printf(" Starting Station: %s \n", stationAtocha.stationName);
System.out.printf(" ============================================\n");
System.out.printf(" (1) - Establish Route\n");
System.out.printf(" (2) - Automatic Connect Route\n");
System.out.printf(" (3) - Manual Switch Over p1\n");
System.out.printf(" (4) - Manual Switch Over p2\n");
System.out.printf(" Select 0 to EXIT\n\n\n");
while ((0 != stationAtocha.optionInExecution) || (false == stationAtocha.inExecution))
{
System.out.printf("> Insert option: ");
stationAtocha.optionInExecution = in.nextInt();
stationAtocha.inExecution = true;
switch(stationAtocha.optionInExecution)
{
case 1:
if (true == Route1.Established)
{
System.out.printf("[NOK] Route is already Established\n\n");
}
else
{
if (true == Route1.establishRoute())
{
System.out.printf("[OK] The Route has been Established\n\n");
}
else
{
System.out.printf("[NOK] The Route is not Connected\n\n");
}
}
break;
case 2:
if (true == Route1.isConnected)
{
System.out.printf("[NOK] Route is already Connected\n\n");
}
else
{
Route1.autoConnectPath();
System.out.printf("[OK] Route has been Connected\n\n");
}
break;
case 3:
if (false == Route1.Established)
{
Point1.changePosition();
System.out.printf("[OK] Position of P1 has been Changed\n\n");
}
else
{
System.out.printf("[NOK] Route is Established. Impossible switch point1\n\n");
}
break;
case 4:
if (false == Route1.Established)
{
Point2.changePosition();
System.out.printf("[OK] Position of P2 has been Changed\n\n");
}
else
{
System.out.printf("[NOK] Route is Established. Impossible switch point2\n\n");
}
break;
case 0:
break;
default: stationAtocha.optionInExecution = 0;
break;
}
}
}
}
abstract public class Path {
/************ Attributes ****************/
protected boolean isConnected;
/* Importante. Como los atributos son objetos, se debe reservar memoria.
En estacion igualamos los points creados a estos objetos para asociarlos */
Point point1 = new Point();
Point point2 = new Point();
/************ Methods ****************/
protected Path ()
{
if ((true == point1.hasRightPosition()) && (true == point2.hasRightPosition())) NULLPOINTEREXCEPTION!!!!!!
{
isConnected = true;
}
else
{
isConnected = false;
}
}
abstract protected void autoConnectPath();
}
public class Route extends Path {
/************ Attributes ****************/
public boolean Established;
/************ Methods ****************/
protected Route() NULLPOINTEREXCEPTION!!!!!!
{
Established = false;
}
protected void autoConnectPath()
{
if (false == point1.hasRightPosition())
{
point1.changePosition();
}
if (false == point2.hasRightPosition())
{
point2.changePosition();
}
this.isConnected = true;
}
public boolean establishRoute()
{
if (true == isConnected)
{
Established = true;
}
return isConnected;
}
}
public class Point {
public enum pointPosition {GOOD, WRONG};
/************ Attributes ****************/
pointPosition Position;
/************ Methods ****************/
protected Point()
{
Position = pointPosition.WRONG;
}
public boolean hasRightPosition()
{
boolean isInGoodPosition = false;
if (pointPosition.GOOD == this.Position)
{
isInGoodPosition = true;
}
return isInGoodPosition;
}
protected void changePosition()
{
if (pointPosition.GOOD == this.Position)
{
this.Position = pointPosition.WRONG;
}
else
{
this.Position = pointPosition.GOOD;
}
}
}