Skelyvelocirap Posted August 24, 2020 Posted August 24, 2020 Hello. I was working on a teleporter for my mod but ran into an issue. This teleporter is going to be consuming different fluids(the fluid logic and fluid tank isn't implemented yet, ill work on it after this issue is resolved) and each of those fluids will have different values which dictates the max distance you can travel with them. The issue i am having is that i am trying to save those values in a multi-dimensional ArrayList. It is an issue because i cannot seem to be able to get the length of the array, or so from what i can tell. Here is how i am making that work: private static ArrayList<String>[][] fuelTypes; public static void addFuelType(Fluid fuel, int fuelEfficiency, boolean isPrecise) { int arrayIndex = 0; int length = TileEntityTeleporter.fuelTypes[0].length; if(length > 0) { arrayIndex = length; } TileEntityTeleporter.fuelTypes[arrayIndex][2].add(Boolean.toString(isPrecise));// = isPrecise; TileEntityTeleporter.fuelTypes[arrayIndex][1].add(Integer.toString(fuelEfficiency)); TileEntityTeleporter.fuelTypes[arrayIndex][0].add(fuel.getFluid().toString());// = fuel.getFluid(); } addFuelType is being used somewhere else but i don't think that matters. Anyways, i pass in the arguments to make it load and when i attempt to load the fueltypes it give me the error of: java.lang.NullPointerException: null at skelyvelocirap.alieninvasion.tile_entities.TileEntityTeleporter.addFuelType(TileEntityTeleporter.java:129) ~[?:?] {re:classloading} This points to this line: int length = TileEntityTeleporter.fuelTypes[0].length; Please help me! I am not sure why its not working, i have tried multiple different things and none of which are working. I feel like im doing a very basic java error but i can't tell what it is! Any help would be appreciated, thanks! Quote
vemerion Posted August 24, 2020 Posted August 24, 2020 Seems to me like you are never initializing your array, and thus it is null. Quote
Draco18s Posted August 24, 2020 Posted August 24, 2020 (edited) ArrayList<String>[][] That's not an array list. That's an array of arrays of arraylists. https://www.w3schools.com/java/java_arraylist.asp But yes, as vemerion is right. Its not initialized. But all of its sub-values aren't initialized either. Edited August 24, 2020 by Draco18s Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Skelyvelocirap Posted August 24, 2020 Author Posted August 24, 2020 3 minutes ago, vemerion said: Seems to me like you are never initializing your array, and thus it is null. Oh, whoops. Never realized that! 1 minute ago, Draco18s said: ArrayList<String>[][] That's not an array list. That's an array of arrays of arraylists. https://www.w3schools.com/java/java_arraylist.asp Hmmm, then how can i do this? Id prefer having everything in one array. I tried using objects but i don't quite understand how to make them in java. Quote
Draco18s Posted August 24, 2020 Posted August 24, 2020 Use a custom data class, then ArrayList<MyCustomFuelDetails> Smashing arrays into other arrays so you can store stringified booleans makes no sense. Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Skelyvelocirap Posted August 26, 2020 Author Posted August 26, 2020 Ok, sorry, i had to go yesterday. Anyways, what exactly do you mean by that? What are data classes? I don't think i have heard of this term yet? Do I simply create a class for my fuel? And how would i use that for an array? Quote
Draco18s Posted August 26, 2020 Posted August 26, 2020 public class MyCustomFuelDetails { // stuff } A "data class" is, unsurprisingly, a class that holds data. In this case, the three values about your fuel that all have a tight relationship to each other. Eg: public class MyCustomFuelDetails { public bool someBool; public int someInt; public Fluid someFluid; } Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Skelyvelocirap Posted August 26, 2020 Author Posted August 26, 2020 Hmmmm, ok. I think i get it. So, i simply make one of those classes and make an array of this class. I simply call the array and when adding a new type of fuel, i simply change the values a bit from where i'm calling it and feed that to the array. Seems simple enough, ill see what i can do! Quote
Skelyvelocirap Posted August 26, 2020 Author Posted August 26, 2020 Hmmm... Ok so i ran into another issue. How exactly would i check for the fluid? I know of one way, looping through all the classes and checking what type of fluid they hold but that would be fairly inefficient. I also thought of making a new instance of that class with the correct fuel and using "contains()" with "equals()" but that would require me to know all of the other values in advance. Not so sure how else i can do it. Is there maybe a function i could use to check for only the fuel in those classes? Quote
Draco18s Posted August 26, 2020 Posted August 26, 2020 (edited) Sounds like you want a dictionary. Er, (Hash)Map in Java Edited August 26, 2020 by Draco18s Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
Skelyvelocirap Posted August 26, 2020 Author Posted August 26, 2020 7 minutes ago, Draco18s said: Sounds like you want a dictionary. Er, (Hash)Map in Java Hmmm... how would a hashmap help? Quote
Draco18s Posted August 26, 2020 Posted August 26, 2020 That lets you map Fluid -> Data. You have O(1) lookup queries on the key Quote Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
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.
Note: Your post will require moderator approval before it will be visible.