This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2012/10/17

problem with unexpected return

1
2
3
erdelf erdelf

2012/10/17

#
how can the str return null?
                for(int i = 1; ; i++)
                {
                    String item = items[i];
                }
danpost danpost

2012/10/18

#
The 'for' loop does not know when to stop processing; so, once the items.length is reached the next item is 'null'. Use
for (int i = 1;  i < items.length; i++)
And, I hope you are aware that the first item in the 'items' array will not be processed if starting 'i' at '1' instead of '0'.
erdelf erdelf

2012/10/18

#
ok, I only copied the necessary part. Shouldn't it return an java.lang.ArrayIndexOutOfBoundsException? btw. this returns null as well.
erdelf erdelf

2012/10/18

#
I still cant make it to not return null
danpost danpost

2012/10/18

#
Then either you have not set all elements (or none) in the array to valid String objects or you nullified one or more before getting to this code. First try adding
System.out.println("Element " + i + ": " + items[i]);
inside the loop to see where in the loop you are getting 'null' from. That might give you an idea of where to look in your code for the problem.
erdelf erdelf

2012/10/18

#
here the code of this part:
            if(items.length > 1)
            {
                for(int i = 1;  i < items.length; i++)
                {
                    String item = items[i];
                    if(item != "null") // still returning null in the if method
                    {
                       // more code
                    }
                }
            }
danpost danpost

2012/10/18

#
The outer 'if' block is totally unneccessary, as the 'for' will not execute even one time if that were the case. Try the 'System.out' statement above and post back results of printout.
erdelf erdelf

2012/10/18

#
Element 1: null
danpost danpost

2012/10/18

#
Show the code where you load the array with data.
erdelf erdelf

2012/10/18

#
I execute the following method two times before:
    public static void addItem(String newItem)
    {
        int x = items.length;
        items = new String[items.length + 1];
        items[x] = newItem;
    }
danpost danpost

2012/10/18

#
There is the problem. Everytime you call this method you lose all the data that was in the array. Line 4 creates a NEW array with 'null' elements.
erdelf erdelf

2012/10/18

#
oh damn, I see. I forgot how to add an element to an array. Could you help my brain?
danpost danpost

2012/10/18

#
I think there might be an easier way, but the following should work:
public static void addItem(String newItem)
{
    int x = items.length;
    String[] temp = new String[x];
    for (i = 0; i < x; i++) temp[i] = items[i];
    items = new String[x + 1];
    for (i = 0; i < x; i++) items[i] = temp[i];
    items[x] = newItem;
}
Gevater_Tod4711 Gevater_Tod4711

2012/10/18

#
do you want the string newItem to be at every position in the array or only at the last? If you want it to be at all positions of the array use this:
    public static void addItem(String newItem)  
    {
        int x = items.length;  
        items = new String[items.length + 1];  
        for (int i = 0; i < items.length; i++) {
            items[i] = newItem;
        }
    }
If you want the newItem string only at the last position use this:
    public static void addItem(String newItem)  
    {  
        int x = items.length;
        String[] temp = items;
        items = new String[items.length + 1];  
        for (int i = 0; i < temp.length; i++) {
            items[i] = temp[i];
        }
        items[items.length-1] = newItem;
    }  
Gevater_Tod4711 Gevater_Tod4711

2012/10/18

#
oh sorry I havent seen danpost has just postet an answer
There are more replies on the next page.
1
2
3