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)

java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Applet.java:141)
at Deck.<init>(Deck.java:38) //line 6 first block

What is the statement at line 38 doing?
If your code is not an applet running in a browser, there is no AppletContext object that can be called to get the document base.

Wow, I can't believe I did that. I apologize for the error; line 38 is line 2 of deck constructor which is:

cardBack = a.getImage( a.getDocumentBase(), imgPath + "back.gif");

Does your code execute OK now?

Sadly no, the code is unchanged. The mistake I fixed was in my description of my programming error.

Then this is still true:
If your code is not an applet running in a browser, there is no AppletContext object that can be called to get the document base.

and don't mixing AWT Applet with Swing Methods, use todays JApplet rather than pre-historic Applet

I don't think changing to the Swing version will create the needed AppletContext/AppletStub.

If you want to use an applet outside of a browser, you need to call the setStub() method with an AppletStub that will provide the services that an applet needs.

Thank you very much. That part of the code (Deck Constructor) was given to us by our professor for a stand-alone applet. Not sure if I'm relieved or horrified, but now I know I should write my own version of it.

So that's what I've been doing for the past day. I have converted the Deck constructor from image to ImageIcon, which produced this

public Deck(String imgPath) {
      cardBack = new ImageIcon("images/back.gif","The back of the card");
      //still having problems here

      for (int k = 0; k < deck.length; k++) {  // Make the cards
          deck[k] = new Card( k );
          String s = imgPath + deck[k].toString() + ".gif";
          deck[k].setImage(new ImageIcon(s,"The front of the card"));

      }
   } // Deck()

However when I compile it (as an applet, not for a web browser), I get this error:

java.lang.ClassCastException: Deck cannot be cast to java.applet.Applet
        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)

Thank you for pointing me in the right direction

Deck cannot be cast to java.applet.Applet

If the Deck object does NOT extend Applet, then you can not cast it to be an Applet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.