Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

java.lang.NullPointerException

Baka_yarouBaka_yarou Member Posts: 13
Hi everyone (first post yay!)

Sorry if this problem is so simple and trivial but it's really bugging me :( lol

I've been trying to create an array of objects and when I compile it, there's no errors but when I run it it gives me

"Exception in thread "main" java.lang.NullPointerException
at Pack.main(Pack.java:23)"

Which, as you'll see from the code I've posted along with this, is when I try to write to the array. I understand this could be because there is a NULL value in my array, but when I've tried to initialise it, I still get this error. Anyway, if anyone can see what I'm obviously doing wrong :P please point it out to me.

Thanks.


[code]
import javax.swing.*;

public class Pack extends Card{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Card[] fDeck;
fDeck = new Card[53];

int i;
int k;

String cs = " ";
String cv = " ";

for (k = 0; k<53; k++)
{
fDeck[k].sN = "00";
fDeck[k].back = new ImageIcon("JK.png");
fDeck[k].front = new ImageIcon("JK.png");
}

for (i=1 ; i <53; i++)
{

int pip;
int divCheck;
divCheck= i % 13;
pip = i / 13;
if (divCheck == 0)
{
pip=pip-1;
}//end if
switch (pip)
{
case 0: cs = "C"; break;
case 1: cs = "D"; break;
case 2: cs = "H"; break;
case 3: cs = "S"; break;
default: System.out.println
("Invalid Suit");break;

}//end switch



pip = i % 13;

switch (pip)
{
case 0: cv = "K"; break;
case 1: cv = "A"; break;
case 2: cv = "2"; break;
case 3: cv = "3"; break;
case 4: cv = "4"; break;
case 5: cv = "5"; break;
case 6: cv = "6"; break;
case 7: cv = "7"; break;
case 8: cv = "8"; break;
case 9: cv = "9"; break;
case 10: cv = "T"; break;
case 11: cv = "J"; break;
case 12: cv = "Q"; break;
default: System.out.println("Invalid card");break;

}//end switch

fDeck[i].sN = cs+cv;


System.out.print("Card Value at "+i+"is "+fDeck[i].sN+"
");
} //end for

}//end main

}[/code]
[code]

import javax.swing.*;

public class Card {

String sN;

Icon back = new ImageIcon("JK.png");
Icon front = new ImageIcon("JK.png");
}
[/code]

Comments

  • silveredge52silveredge52 Member Posts: 198
    Baka_yarou,
    Good code, however need to instantiate a Card for every entry in the array. i.e. fDeck[k] = new Card(); before the statement that is trying to reference a Card field.

    Regards
  • Baka_yarouBaka_yarou Member Posts: 13
    Ahh ok :) so I just need to do a for loop declaring that every field in the array is a new card? Nice, and I suppose that makes sense :D

    Thanks very much for your help!
  • silveredge52silveredge52 Member Posts: 198
    That is one approach.

    Alternatively, your code already has a loop, e.g.
    [color=Blue]for (k = 0; k<53; k++)
    { ... [/color]
    where the previously mentioned instantiation could be used.
  • Baka_yarouBaka_yarou Member Posts: 13
    Yeah, that's what I did :D It's working now. I was wondering if you could help me with something else :P

    I'm trying to create a method in the Pack class called "shuffle" which takes in an array of Card and an int value, rearranges the order of the array and then returns the array. However, being new to Java, I never know what to put at the start of the method, ie "public ??? methodname(params)". I've done C++ before (4 years ago) and it was just a case of using pointers with the parameters. Anyway, this is what I've got so far. Whenever I compile this I get "'class' or 'interface' expected" which I'm sure is to do with my declaration of the method.

    I've tried public static, public void, public Card, public Card[] but I always get the same error. :(

    [code]
    public static shuffle(Card[] toBe, int c)
    {

    int k;

    for (k=1; k<c; k++)
    {
    Random ran = new Random();
    int rC = ran.nextInt(c);

    while( rC == 0)
    {
    ran = new Random()
    rC = ran.nextInt(c);
    } //end while

    toBe[0] = toBe[k];
    toBe[k] = toBe[rC];
    toBe[rC] = toBe[0];

    }//end for

    return toBe;
    }//end shuffle

    }
    [/code]

    Thanks again for your previous help, and any help you can give me on this problem.

  • silveredge52silveredge52 Member Posts: 198
    Where the method is located is important. I assume it is before the bracket that delimits the Pack class.
    The following should get past the compile error.[color=Blue]
    private static Card[] shuffle(Card[] toBe, int c)
    {
    ...[/color]
    What precedes the method name is the return type, an array of Cards in this case. No return value requires the keyword [color=Blue]void[/color].

    Keyword static is used because a Pack object has not been instantiated and a static member (method) is available from the Pack class, i.e. the main method.
  • Baka_yarouBaka_yarou Member Posts: 13
    Yeah, that's where I was going wrong. I had it outside the Pack class delimiter, when I meant to put it outside the main delimiter.

    Thanks again. I've had to declare it "public static Card[] shuffle( )" as it wasn't happy with the static main trying to call a non-static (can't remember the full error because I've changed it now) but it's working now. Thank you, yet again, you've saved my project (my tutor couldn't help me with my original problem). :D


Sign In or Register to comment.