I am running into a problem in a card dealing program. When the Deck Constructor tries to run one of the methods associated with the Applet class, the program will fail with an error set typical to running an application as an applet. This is a school project, and the deadline for it has already passed, but I would like to understand where this is going wrong.
Deck Constructor, given and assumed to be correct
public Deck(Applet a, String imgPath) {
cardBack = a.getImage( a.getDocumentBase(), imgPath + "back.gif");
//this is there it goes nuts
a.showStatus("Please wait while card images are loading. ");
for (int k = 0; k < deck.length; k++) { // Make the cards
deck[k] = new Card( k );
String s = imgPath + deck[k].toString() + ".gif";
Image img = a.getImage(a.getDocumentBase(), s);
deck[k].setImage(img);
}
} // Deck()
Applet Constructor which extends Applet and implements ActionListener
public CardApplet()
{
init();
deck=new Deck(this,"\\images\\");//I also tried super.this to no avail
deal.addActionListener(this);
hit.addActionListener(this);
initialBet.addActionListener(this);
}
public void init()
{
setLayout(new BorderLayout());
buildUpper();
add("North",upperPart);
buildCards();//the build() methods follow the pattern of buildUpper()
buildFirstBet();
add("West",leftPane);
buildRaiseBet();
add("East",rightPane);
buildBottomPart();
add("South",lowerPane);
}
public void buildUpper()//Generate Buttons
{
upperPart.setLayout(new BorderLayout());
upperPart.add("North",prompt);
upperPart.add("West",deal);
upperPart.add("East",hit);
}
Error set
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Applet.java:141)
at Deck.<init>(Deck.java:38) //line 6 first block
at CardApplet.<init>(CardApplet.java:49) //line 4 second block
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:662)