Tempo
Tempo

Reputation: 35

Error on a Specific Line Number - Android Studio

No matter how I change that specific line, even if I put nothing in it, or making it a comment, or delete the code all together, the error message keeps telling me that this line causes the error. I even tried deleting the class and copying it all into a new one but still same line numebr is an error.

The code with the error line (109) highlighted:

package com.example.president;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Game extends AppCompatActivity implements View.OnClickListener {


    private Manager gManager;
    Player p1,p2,p3;
    private ImageView[] hand;
    private ImageView[] curr;
    private ImageView[] next= new ImageView[3];
    private int[] turn = {0, 1, 2};
    private int cthrow;
    public int[] cards =
            {
                    R.drawable.c3,
                    R.drawable.c4,
                    R.drawable.c5,
                    R.drawable.c6,
                    R.drawable.c7,
                    R.drawable.c8,
                    R.drawable.c9,
                    R.drawable.c10,
                    R.drawable.c11,
                    R.drawable.c12,
                    R.drawable.c13,
                    R.drawable.c1,
                    R.drawable.c2,
                    R.drawable.h3,
                    R.drawable.h4,
                    R.drawable.h5,
                    R.drawable.h6,
                    R.drawable.h7,
                    R.drawable.h8,
                    R.drawable.h9,
                    R.drawable.h10,
                    R.drawable.h11,
                    R.drawable.h12,
                    R.drawable.h13,
                    R.drawable.h1,
                    R.drawable.h2,
                    R.drawable.s3,
                    R.drawable.s4,
                    R.drawable.s5,
                    R.drawable.s6,
                    R.drawable.s7,
                    R.drawable.s8,
                    R.drawable.s9,
                    R.drawable.s10,
                    R.drawable.s11,
                    R.drawable.s12,
                    R.drawable.s13,
                    R.drawable.s1,
                    R.drawable.s2,
                    R.drawable.d3,
                    R.drawable.d4,
                    R.drawable.d5,
                    R.drawable.d6,
                    R.drawable.d7,
                    R.drawable.d8,
                    R.drawable.d9,
                    R.drawable.d10,
                    R.drawable.d11,
                    R.drawable.d12,
                    R.drawable.d13,
                    R.drawable.d1,
                    R.drawable.d2,
                    R.drawable.j1,
                    R.drawable.j2
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        p1 = new Player("p1");
        p2 = new Player("p2");
        p3 = new Player("p3");

        this.hand = new ImageView[18];
        String str;
        int resId;
        int i;
        for (i=0;i<hand.length;i++)
        {
            str = "card"+i;
            resId = getResources().getIdentifier(str, "id", getPackageName());
            hand[i]= (ImageView)findViewById(resId);
            hand[i].setOnClickListener(this);
        }
        for (i=0; i<4; i++)
        {
            str="board"+i;
            resId = getResources().getIdentifier(str, "id", getPackageName());
            curr[i]= (ImageView)findViewById(resId);
            curr[i].setOnClickListener(this);
        }

        this.gManager = new Manager(this, p1, p2, p3);

        this.gManager.handingDeck(p1, p2, p3);
[[[LINE 109]]]
        startGame(p1, p2, p3);

    }

    public void startGame(Player p1, Player p2, Player p3) {

        Player p=p1;
        int i;
        for (i=0; i < 18; i++) {
            hand[i].setImageResource(cards[p.getHand().get(i).getIndex()]);
        }
        String text = p1.getHand().toString();
        TextView change = (TextView)findViewById(R.id.textView);
        change.setText(text);
    }

    @Override
    public void onClick(View v) {
        int i, cnum=0, t=0, resId;
        boolean found = false;
        Player p=p1;
        cthrow=1;
        for (i = 0; i < 18 && (!(found)); i++)
        {
            if (v.getId() == hand[i].getId())
            {
                String str="card"+cnum;
                resId=getResources().getIdentifier(str, "id", getPackageName());
                next[turn[t]]= (ImageView)findViewById(resId);
                found=true;
                curr[cthrow].setImageResource(cards[p.getHand().get(i).getIndex()]);
                p.getHand().remove(i);
                next[turn[t]].setVisibility(View.INVISIBLE);
                if(cnum<10)
                    cnum=18-cnum;
                else
                    cnum=18-cnum+1;
                cthrow++;
            }
        }
    }
}

As you can see the line is even empty.

The error

Upvotes: 0

Views: 401

Answers (2)

Newbie
Newbie

Reputation: 4819

As pointed out in the comments the error happens actually here:

HERE>> curr[i]= (ImageView)findViewById(resId);
curr[i].setOnClickListener(this);

But it will happen at any line where you use curr as it is not initialized.

For your specific error: When an error line do not change even if you intentionally change your code the only plausible answer is only one. The actual running code is an old one. So you need to refresh it. How? it depends on where / how you are running it, but a straight forward approach will be to follow this steps incrementally (testing if something changes meanwhile):

  • Rebuild your project (using your ide rebuild button or by command line)
  • Clean build your project (ide or cmd as above)
  • Redeploy your application (in case of an android application 're-install' it on the device where you are testing; emulator or phone)
  • Uninstall and delete the currently running application (and repeat point 2 & 3)
  • Reboot your test device (emulator / phone and repeat point 2 & 3)
  • Reboot your IDE (if you are not sure that all its processes rebooted, reboot your pc)
  • Delete any build intermediate / cache file your ide may have stored (and repeat 2 & 3)
  • Nothing worked. Attempt different combinations of the above solutions
  • If you are here, start thinking about the most dumb stuff and get creative, like:
    • am I installing the app?
    • have I stored the modified files?
    • is the phone connected?
    • am I modifying the files in the right project?
    • ...

Hope this can help

Upvotes: 1

Krishna sheladiya
Krishna sheladiya

Reputation: 451

you have initialized array in a wrong manner, by using new it will initialize array and create memory for that array.

-- so right way to initialize and add values to array is following.

public int[] cards = new int[]{ add your Drawbles here };

Upvotes: 0

Related Questions