-2

I am creating a Chess program on NetBeans using jButtons as squares, and my java knowledge is limited to what I have learnt at school.

So this line

int verticalValue = Integer.parseInt(newButton.substring(1,1));

returns a nullPointerException and I can't figure it out whatsoever. Here is the relevant code:

static void pawnMovement(JButton but){
    String buttonName = but.getName();
    String newButton = buttonName;
    int verticalValue = Integer.parseInt(newButton.substring(1,1));

The names of all buttons are in the format letterNumber, so I don't see why this shouldn't work.

Thanks!

Grim
  • 1,938
  • 10
  • 56
  • 123
dunno
  • 1
  • simply print the value of `newButton` and see it yourself though substring is innocent in this case – Pavneet_Singh Apr 24 '18 at 17:34
  • Seems to be a classical NullPointerException... What does `but.getName();` return? Certainly, it returns *null* and thats your problem... Chech this before using the variable `newButton` or `buttonName` – 0x1C1B Apr 24 '18 at 17:34

2 Answers2

2

This code should produce a NumberFormatException as the string from substring(1, 1) will always be empty, unless newButton is null because it hasn't been set.

I would check in your debugger that is has been set. I would also ensure you are trying to parse at least 1 character.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • It's really amazing to see that your answer appears more valuable to people even though you _technically didn't_ answer the question. Reputation, indeed, has a huge role to play, I guess. No offence. – Pankaj Singhal Apr 24 '18 at 17:50
  • @PankajSinghal maybe you know something I don't but often new developers don't read the error message and don't realise they are not all the same. I made the point that if the OP believes the value can't be null perhaps they misread the exception. – Peter Lawrey Apr 24 '18 at 20:02
  • Totally agree with you, and it is the reality what you have said. But I was making a different point. :) – Pankaj Singhal Apr 25 '18 at 02:30
  • @PankajSinghal my impression is that upvoting had an element of randomness. – Peter Lawrey Apr 25 '18 at 06:35
  • Hmm. Maybe. But, I seriously didn't need a sympathetic upvote (if you did it). – Pankaj Singhal Apr 25 '18 at 06:57
  • @PankajSinghal I upvoted because you might be correct (there is not enough information in the question IMHO) – Peter Lawrey Apr 25 '18 at 15:14
  • Yeah, I sure did a dangerous thing, in the computing world, of making an _assumption_ – Pankaj Singhal Apr 25 '18 at 15:24
1

When you do a new JButton("name") - it sets the variable JButton.text as name. Hence, but.getText() should work for you.

In your case, but.getName() returns NULL because you have NOT done but.setName() first. but.setName() is required for but.getName() to work.

Hence, buttonName & newButton are NULL.

Hence, when you do newButton.substring(1,1) - it causes NPE because newButton is NULL

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86