Jump to content

Java final projec trouble

super_teabag
Go to solution Solved by Ghost,

You are getting a null pointer exception because you are trying to check a position of the array list that doesn't exist.

 

Use:

ArrayList.isEmpty();

Having a bit of trouble with this method that i'm trying to create a method for a project.

public void show (){        String result = "";        if(ItemsHeld.get(1)==null){            msg = "The player is not holding anything.";        }        else{            for(Item objects: ItemsHeld){                result += objects.getName()+ " ";            }            msg = "You are holding " + result;        }    } 

This method is designed to check if there's items in a player inventory.

 

The Item has 3 methods in it's class "getname()" returns the name of the object.

getLongdescription() which returns the long descriptions of the item.

and getweight() which gets the weight of the object as an integer.

 

The items are stored in an arraylist called "ItemsHeld".

 

At this point in time there's nothing in the arraylist of Items.

 

There's two scenarios that could happen one you can return all the items in the players inventory to at msg.

Secondly, the player has no items in his/her inventory and returns 'you don't have any items.'

 

The compiler is giving me an error about a null pointer, saying something about the size of the array list

Idk how to check if a players inventory doesn't contain anything without some kind of error popping up.

 

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you aware that the first thing in an array is position 0 and not 1?

 

Also:

 

You can check the length of an Array in Java.

DayZ Forum Moderator, DayZ Developer, ARMA 3: 2017 Developer, System-Admin, Gameserver-Admin, always interested to learn something new as well as new people.

Link to comment
Share on other sites

Link to post
Share on other sites

You are getting a null pointer exception because you are trying to check a position of the array list that doesn't exist.

 

Use:

ArrayList.isEmpty();

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
Share on other sites

Link to post
Share on other sites

yeah i've tried that first, and i knew that the first index starts at 0. Such as itemsheld.get(0) will return the first indexed item.

 

i've also tried ItemsHeld.size()=0 in my if statement, but here's the error i keep getting.

 

at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
at java.util.ArrayList.add(ArrayList.java:426)
at Game.show(Game.java:218)
at Game.main(Game.java:269)
 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Since the items are stored in an array list you can use .size()

if (itemList.size() > 0) {    //...} 

How would that be better than what he already has: "for(Item objects: ItemsHeld)" will do the same as the if statement if there are no Items in the ArrayList.

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
Share on other sites

Link to post
Share on other sites

yeah i've tried that first, and i knew that the first index starts at 0. Such as itemsheld.get(0) will return the first indexed item.

 

i've also tried ItemsHeld.size()=0 in my if statement, but here's the error i keep getting.

Use == for comparisons. A single = is used for declerations.

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
Share on other sites

Link to post
Share on other sites

How would that be better than what he already has: "for(Item objects: ItemsHeld)" will do the same as the if statement if there are no Items in the ArrayList.

Doing the check is not the same as his current code. Also you can negate it or add an else depending on what you need from it so it's not the same as a for statement either.

 

Anyway, I forgot about isEmpty and edited my post. It's a better option than checking size anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

Doing the check is not the same as his current code. Also you can negate it or add an else depending on what you need from it so it's not the same as a for statement either.

 

Anyway, I forgot about isEmpty and edited my post. It's a better option than checking size anyway.

This code requires no size()

public void show (){        if(!ItemsHeld.isEmpty()){            msg = "You are holding ";            for(Item objects: ItemsHeld){                msg += objects.getName()+ " ";            }            return;        }        msg = "The player is not holding anything.";    }

Feel free to PM for any water-cooling questions. Check out my profile for more ways to contact me.

 

Add me to your circles on Google+ here or you can follow me on twitter @deadfire19.

Link to comment
Share on other sites

Link to post
Share on other sites

This code requires no size()

I am aware. You either didn't read what I said or didn't understand it.

Link to comment
Share on other sites

Link to post
Share on other sites

yeah i've tried that first, and i knew that the first index starts at 0. Such as itemsheld.get(0) will return the first indexed item.

 

i've also tried ItemsHeld.size()=0 in my if statement, but here's the error i keep getting.

 

at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)

at java.util.ArrayList.add(ArrayList.java:426)

at Game.show(Game.java:218)

at Game.main(Game.java:269)

 

 

What is the code at line 218?

Link to comment
Share on other sites

Link to post
Share on other sites

@Midnight @madknight3 @Ghost

Thanks for all the help. that "isEmpty()" method is awesome I didn't know it existed as part of the arraylist library.

 

I got the errors worked out, and the method works now. Thanks for the help guys!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×