I got a program that based on what the type of car I'm working with, it will call a set number of methods. One of the type of cars that I'm working with will go through this snippet of code:
if (infocar[0] == "E") {
if (ElectricCar.isValidPlate(infocar[1]) == true)
{
int elecPower = Integer.parseInt(infocar[3]);
double batCharge = Double.parseDouble(infocar[4]);
if (ElectricCar.isValidelectricPower(elecPower) == true)
{if (ElectricCar.isValidbatteryCharge(batCharge) == true){
ElectricCar miElectricCar = new ElectricCar(infocar[1], infocar[2], elecPower, batCharge);
electricCityCars[cochesE] = miElectricCar;
cochesE++;}
else{}
}else {}
}else{}
} else{}
The idea is that if the inputs are correct, and object of class ElectricCar will be created and then added to the array electricCityCars[];
Then, I create another method, which will go through the array of electric cars and invoke another method:
for (int i=0; i<=25; i++) {
EPower = electricCityCars[i].getElectricPower();
TotalPowerElectric += EPower;
}
Problem is that on the main, Im getting NullPointerException because Im calling a null object on .getElectricPower(), but isn't the object getting created properly?
While writing this question, I noticed that I should handle the case where there are no electric cars, but atleast with the file from which I'm extracting the different car types, there is 1 electric car.
The full code if you need it is as follows :
<pre>package P3;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class P3a {
public static void main(String[] args)throws FileNotFoundException {
String nombrefichero = args[0];
String plate3 = "6288LYM", manufacturer3="Ford"; int power3=110; float batteryCharge3 = 120.5F;
String plate4 = "07451JMR", manufacturer4 = "Seat"; int power4=60; float batteryCharge4 = 10.1F;
String plate5 = "3400JXK", manufacturer5 = "Peugeot"; int power5=70; float batteryCharge5 = 50F;
String plate6 = "0041LDR", manufacturer6 = "Seat"; int power6 = 20; float batteryCharge6 = 10.1F;
ElectricCar miElectricCar1 = new ElectricCar("1111KLS", "SEAT", 220, 30.5);
ElectricCar miElectricCar2 = new ElectricCar();
miElectricCar2.setPlate("2222LSX");
miElectricCar2.setManufacturer("FORD");
miElectricCar2.setElectricPower(220);
miElectricCar2.setBatteryCharge(30.5);
String s1 = miElectricCar1.toText();
String s2 = miElectricCar2.toText();
System.out.println(s1);
System.out.println(s2);
if (ElectricCar.isValidPlate("6288LYM") == true)
{if (ElectricCar.isValidelectricPower(110) == true)
{if (ElectricCar.isValidbatteryCharge(120.5) == true)
{ElectricCar miElectricCar3 = new ElectricCar("6288LYM", "Ford", 110, 120.5);}
}
}
if (ElectricCar.isValidPlate("07451JMR") == true)
{if (ElectricCar.isValidelectricPower(60) == true)
{if (ElectricCar.isValidbatteryCharge(10.1) == true)
{ElectricCar miElectricCar3 = new ElectricCar("07451JMR", "Seat", 60, 10.1);}
}
}
if (ElectricCar.isValidPlate("3400JXK") == true)
{if (ElectricCar.isValidelectricPower(70) == true)
{if (ElectricCar.isValidbatteryCharge(50) == true)
{ElectricCar miElectricCar3 = new ElectricCar("3400JXK", "Peugeot", 70, 50);}
}
}
if (ElectricCar.isValidPlate("0041LDR") == true)
{if (ElectricCar.isValidelectricPower(20) == true)
{if (ElectricCar.isValidbatteryCharge(10.1) == true)
{ElectricCar miElectricCar3 = new ElectricCar("0041LDR", "Seat", 20, 10.1);}
}
}
System.out.println("Hola");
new P3a().readCityCarsFile(nombrefichero);
int Power = new P3a().computeTotalPower();
System.out.println(Power);
System.out.println("Hola");
File ficheroescritura = new File("ElectricCarsOutput.txt");
PrintWriter pw = new PrintWriter(ficheroescritura);
for (int l=0; l<=25; l++) {
electricCityCars[l].increaseBatteryChargeLevel(10);
pw.println(electricCityCars[l].toText());
}
pw.close();
}
static ElectricCar[] electricCityCars = new ElectricCar[25];
static CombustionCar[] combustionCityCars = new CombustionCar[25];
static HybridCar[] hybridCityCars = new HybridCar[25];
private static int cochesC = 0;
private static int cochesE = 0;
private static int cochesH = 0;
public void readCityCarsFile (String fichero) {
File file = new File(fichero);
Scanner inputfichero = new Scanner(fichero);
do {
String linea = inputfichero.nextLine();
if (linea.startsWith("#")) {
continue;
}
else {
String[] infocar = linea.split(";");
if (infocar[0] == "C") {
if (CombustionCar.isValidPlate(infocar[1]) == true)
{
int mecPower = Integer.parseInt(infocar[3]);
if (CombustionCar.isValidmechanicalPower(mecPower) == true){
CombustionCar miCombustionCar = new CombustionCar(infocar[1], infocar[2], mecPower);
combustionCityCars[cochesC] = miCombustionCar;
cochesC++;} else{}
}else{}
} else{}
if (infocar[0] == "E") {
if (ElectricCar.isValidPlate(infocar[1]) == true)
{
int elecPower = Integer.parseInt(infocar[3]);
double batCharge = Double.parseDouble(infocar[4]);
if (ElectricCar.isValidelectricPower(elecPower) == true)
{if (ElectricCar.isValidbatteryCharge(batCharge) == true){
ElectricCar miElectricCar = new ElectricCar(infocar[1], infocar[2], elecPower, batCharge);
electricCityCars[cochesE] = miElectricCar;
cochesE++;}
else{}
}else {}
}else{}
} else{}
if (infocar[0] == "H") {
if (HybridCar.isValidPlate(infocar[1]) == true)
{
int mhpower = Integer.parseInt(infocar[3]);
int mepower = Integer.parseInt(infocar[4]);
double bathCharge = Double.parseDouble(infocar[5]);
if (HybridCar.isValidmechanicalPower(mhpower) == true)
{if (HybridCar.isValidelectricPower(mepower) == true)
{if (HybridCar.isValidbatteryCharge(bathCharge) == true){
HybridCar miHybridCar = new HybridCar(infocar[1], infocar[2], mhpower, mepower, bathCharge);
hybridCityCars[cochesH] = miHybridCar;
cochesH++;}
else{}
}else{}
}else{}
}else{}
}else {}
}
} while(inputfichero.hasNext() == true);
inputfichero.close();
}
public static int computeTotalPower () {
int EPower = 0;
int MPower = 0;
int TotalPowerElectric = 0;
int TotalPowerMechanical = 0;
int TotalPower = 0;
for (int i=0; i<=25; i++) {
EPower = electricCityCars[i].getElectricPower();
TotalPowerElectric += EPower;
}
for (int i=0; i<=25; i++) {
MPower = combustionCityCars[i].getMechanicalPower();
TotalPowerMechanical += MPower;
}
for (int i=0; i<=25; i++) {
EPower = hybridCityCars[i].getElectricPower();
MPower = hybridCityCars[i].getMechanicalPower();
TotalPowerElectric += EPower;
TotalPowerMechanical += MPower;
}
TotalPower = TotalPowerElectric + TotalPowerMechanical;
return TotalPower;
}
}
What I have tried:
I have already tested most methods on the main, such as the "isValid" methods, and again, I've checked that there is atleast 1 electric car on the .txt.
Each array is declared as a global variable outside the main, since we were told to do that.