Jump to content

[1.15.2] issue while trying to get contents of an ArrayList


Skelyvelocirap

Recommended Posts

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! :D

Link to comment
Share on other sites

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 by Draco18s

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Use a custom data class, then ArrayList<MyCustomFuelDetails>

Smashing arrays into other arrays so you can store stringified booleans makes no sense.

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.

Link to comment
Share on other sites

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;
}

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Sounds like you want a dictionary. Er, (Hash)Map in Java

Edited by Draco18s

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.

Link to comment
Share on other sites

That lets you map Fluid -> Data. You have O(1) lookup queries on the key

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.