Eckirion Posted March 21, 2016 Share Posted March 21, 2016 (edited) I'm having a bit of problem whenever I run my code. Exception in thread "main" java.lang.NullPointerException at simulationapp.SimulationApp.main(SimulationApp.java:40) I did not initialize my array of classes with equals to NULL so I'm in a pinch here. Main Class: public class SimulationApp { /** * @param args the command line arguments */ public static void main(String[] args) { Bridge unit = new Bridge(); final float DELTA = 24; int curr = 1; int marker = 1; int x; Random rng = new Random(); int rand; Vehicle[] graze = new Vehicle[15]; Ship[] sail = new Ship[4]; int maxRight = 0; int maxLeft = 0; int maxCanal = 0; for(x = 0; x < 15; x++) { graze[x].setType(); graze[x].setArrivalTime(); setOffTime(graze[x], unit); } for(x = 0; x < 4; x++) { sail[x].setType(); sail[x].setArrivalTime(); } while(curr != DELTA) { if(unit.isCanalEmpty() == true) { for(x = 0; x < graze.length; x++) { if(curr == graze[x].getArrivalTime()) { rand = rng.nextInt(1); if(rand == 0) { unit.addVehicleLeft(graze[x]); ++maxLeft; } else if(rand == 1) { unit.addVehicleRight(graze[x]); ++maxRight; } } } } else if(unit.isBridgeEmpty() == true) { for(x = 0; x < sail.length; x++) { if(curr == sail[x].getArrivalTime()) { unit.addShip(sail[x]); } } } for(x = 0; x < graze.length; x++) { if(getOffTime(graze[x]) == curr) { rand = rng.nextInt(1); if(rand == 0) unit.removeVehicleLeft(); else if(rand == 1) unit.removeVehicleRight(); } } for(x = 0; x < sail.length; x++) { if(getOffTime(sail[x]) == curr) { unit.removeShip(); } } ++curr; } for(x = 0; x < graze.length; x++) { System.out.println("\nVehicle #" +marker +":"); System.out.println("Type: " +graze[x].getOffType()); System.out.println("Arrival Time: " +graze[x].getArrivalTime()); System.out.println("Distance per Hour: " +graze[x].getDistancePerHour()); System.out.println("Off Time: " +getOffTime(graze[x])); ++marker; } marker = 1; for(x = 0; x < sail.length; x++) { System.out.println("\nShip #" +marker +":"); System.out.println("Type: " +sail[x].getSeaType()); System.out.println("Arrival Time: " +sail[x].getArrivalTime()); System.out.println("Distance per Hour: " +sail[x].getDistancePerHour()); System.out.println("Off Time: " +getOffTime(sail[x])); } System.out.println("\nSummary: "); System.out.println("No. of Vehicle on Left Road: " +maxLeft); System.out.println("No. of Vehicle on Right Road: " +maxRight); System.out.println("No. of Ship that passed through the Canal: " +maxCanal); } public static void setOffTime(Vehicle graze, Bridge unit) { graze.offTime = ( graze.getDistancePerHour() * unit.BRIDGE_LENGTH ) + graze.getArrivalTime(); } public static float getOffTime(Vehicle graze) { return graze.offTime; } public static void setOffTime(Ship sail, Bridge unit) { sail.offTime = ( sail.getDistancePerHour() * unit.CANAL_LENGTH ) + sail.getArrivalTime(); } public static float getOffTime(Ship sail) { return sail.offTime; } } Vehicle Class: public class Vehicle { private final int[] ATH = {2, 5, 9, 13, 18, 22}; private float distancePerHour; private int arrivalTime; private String offType; float offTime; public int limit = 13; Random rng = new Random(); public void setType() { final String[] TYPE = {"Car", "Truck", "Bulldozer", "Motorcycle", "Sports Car", "Mountain Bike"}; int randomNum = rng.nextInt(5); this.offType = TYPE[randomNum]; switch(offType) { case "Car": this.distancePerHour = 1f; break; case "Truck": this.distancePerHour = 0.5f; break; case "Bulldozer": this.distancePerHour = 0.2f; break; case "Motorcycle": this.distancePerHour = 0.8f; break; case "Sports Car": this.distancePerHour = 2f; break; case "Mountain Bike": this.distancePerHour = 0.5f; default: this.distancePerHour = 1f; break; } } public void setArrivalTime() { int randChooser = rng.nextInt(5); this.arrivalTime = ATH[randChooser]; } public int getArrivalTime() { return this.arrivalTime; } public String getOffType() { return this.offType; } public float getDistancePerHour() { return this.distancePerHour; } } NOTE: This is where the error points to for(x = 0; x < 15; x++) { graze[x].setType(); <-------- This one. graze[x].setArrivalTime(); setOffTime(graze[x], unit); } To whoever can help me, thanks in advance! Edited March 21, 2016 by Ercki Quote Link to comment Share on other sites More sharing options...
Makaze Posted March 21, 2016 Share Posted March 21, 2016 (edited) I don't know Java super well, but it looks like you referenced this.offTypein your switch condition, but without the this.keyword, so it is looking for a normal variable that hasn't been defined: this.offType = TYPE[randomNum]; switch(offType) { Edited March 21, 2016 by Makaze Quote Link to comment Share on other sites More sharing options...
Primefusion Posted March 21, 2016 Share Posted March 21, 2016 Are you initializing each index in the graze array? If you just declare an array of objects, you'll be left with an array of nulls (Unless Java does this for you automatically, my memory is hazy at best). Something like: for(x = 0; x < 15; x++) { graze[x] = new Vehicle(); // Need to create an object before we can do anything with it graze[x].setType(); graze[x].setArrivalTime(); setOffTime(graze[x], unit); } Quote Link to comment Share on other sites More sharing options...
Griffonite Posted March 21, 2016 Share Posted March 21, 2016 (edited) If you just declare an array of objects, you'll be left with an array of nulls (Unless Java does this for you automatically, my memory is hazy at best). EDIT: This is correct, somehow 'array of objects' didn't register in my mind. Edited March 21, 2016 by Griffonite Quote Link to comment Share on other sites More sharing options...
Eckirion Posted March 21, 2016 Author Share Posted March 21, 2016 @Primefusion: Thank you very much! This really helped. Quote Link to comment Share on other sites More sharing options...
JenniferDavis Posted March 27, 2016 Share Posted March 27, 2016 · Hidden by Nightmare, March 27, 2016 - No reason given Hidden by Nightmare, March 27, 2016 - No reason given May be you are having some problem in array. While recently when I was looking for company that will help me in building a website for me, so he suggested me to must visit the below site hjmt.com. Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.