0

I have a function that 'crafts' products using two String parameters. This is working fine when I put in hard coded strings like 'Wheel' & 'Car'. But it makes my application crash if I try to put in the exact same strings but then provided by an intent.

I already tried to give in variable into the intent instead of a hard coded string. That did not work either. Here is some part of the code. EDIT: Error log now included

    productLeft = getIntent().getStringExtra("PRODUCT LEFT");
    productRight = getIntent().getStringExtra("PRODUCT RIGHT");

    public void craft(String product1, String product2) {
    String[][] Products = factory.getProductList();

    int i = 0;
    while (finalProduct == "") {
        int j;
        for(j = 0; j < 3; j++){
            if (product1 == Products[i][0] || product2 == Products[i][0]) {
                if (product1 == Products[i][1] || product2 == Products[i][1]){
                    finalProduct = Products[i][2];
                }
            }
            i++;
        }
    }
}

enter image description here

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Vlusion
  • 79
  • 1
  • 14
  • Use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 07 '20 at 23:39
  • Sorry, forgot that. Will include that in a minute – Vlusion Nov 07 '20 at 23:40

1 Answers1

1

Problem is with the array index obviously. The array has only four elements and you are fetching index 4, probably in for loop with i variable. But then again I also do not see the role of j in that loop, can't tell without other parts of code.

Bran
  • 71
  • 5
  • The crash indeed had something to do with my loop. Now fixed that and the crash. But still dont understand why it wont take an String provided by an Intent eventhough it is exactly the same. – Vlusion Nov 08 '20 at 12:31
  • 1
    In java you should compare two strings like this **product1.equals(Products[i][0])** not **product1 == Products[i][0]** . **==** compares if they are same object, and strings can have same content but be different objects, like in this example. But anyway it was good because it revealed a flaw in your loop/indexing which you wouldn't notice if the string comparison worked, and that would be a bug that would sooner or later spring out when it would be much more difficult to fix. – Bran Nov 08 '20 at 16:27
  • Ahh, I have heard that one before but could not recall it. Now also fixed THAT problem by using your fix. You really helped me out for such a simple problem. Thank you, will mark this as a awnser now. – Vlusion Nov 08 '20 at 17:10