• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerExceptions on first attempt at JButtons  RSS feed

 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my first attempt at GUI, and I have searched everything I can find on this, and posted the question on other websites. No one seems to know how to help. I am designing/coding a blackjack GUI program and I'm trying to add function to my JButtons. Problem is, I now have 2 NullPointerExceptions and I have no idea why.



userTurn() was originally working in the above class, but I needed to turn it into a separate method for the JButton.


Then I want the Jbutton to call the method again for a new round in the game.



I know that there may be excess code in here, but I figured you would probably want a better idea of what I am trying to do.

I am getting the following exception/stack trace at line 14 in userTurn()

Exception in thread "main" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.GameConsole.gamePlay(GameConsole.java:87)
at blackjackControls.Main.main(Main.java:7)

and then the program continues and after I click the button I get
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:163)
at blackjackControls.ResultPanel$1.actionPerformed(ResultPanel.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
//and then about 30 more "at" lines

I can't for the life of me understand where I am going wrong. If any other code is needed, let me know and I'll be happy to post it. Why am I getting these exceptions and how do I fix it?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the code at GameConsole.java:163 look like? (and post some code before and after for context).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that your user and/or player references are probably null. You need to assign something to these variables first before you call userTurn().

Many of my comments from before still apply here, if you want to make improvements to your code:
1. Format your code properly. Align things so that the structure and the flow of the program is clear.
2. Do not mix UI with game logic. Keep those separate from each other. Your calls to JOptionPane methods should be in a different class. Making changes will, however, require some changes in the way you think about how the flow of the program goes.

The Hollywood Principle is something you should keep in mind, especially when you are doing event-driven programming.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What does the code at GameConsole.java:163 look like? (and post some code before and after for context).


If I were to guess, these are probably the actual code's line numbers:


Larissa Perkins wrote:userTurn() was originally working in the above class, but I needed to turn it into a separate method for the JButton.

That may have fixed an immediate problem but I think it made your design poorer. userTurn() should stay in your GameConsole class. I'm sure there's a way to fix the problem without violating good design principles.

Edit: I may have misunderstood what you wrote. If userTurn() is in fact a method of GameConsole, then you're fine. Just the calls to JOptionPane methods are out of place in that method.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Knute Snortum wrote:What does the code at GameConsole.java:163 look like? (and post some code before and after for context).


If I were to guess, these are probably the actual code's line numbers:


That would be my guess too but the OP says it's occurring on line 14 of the userTurn method which seems highly unlikely as the only deference on that line is user which can't be null first time it is deferenced at line 14 as it is deferenced on the lines shown above - unless he/she has a variable called JOptionPane which isn't initialised (which is even more unlikely).
 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, userTurn() is in the GameConsole class. @Junilu yes, that is the point in the program that it breaks both on the first round an each time the button is pressed afterwards. I made a mistake on which line I typed in. I'm sorry my question was so unclear.

The spacing was fine in the program, but skewed once I pasted in the textbox. I thought I had fixed it but apparently I had not. I'll take a look at the Hollywood Principle. Thank you for the resource.

Why does it break at 163 in the code snippet?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larissa Perkins wrote:Why does it break at 163 in the code snippet?

At what point in the program's execution do you have statements that look like the lines of code below?
These statements need to be executed before the call to userTurn() is made.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related to the Hollywood Principle, which is mainly about loose coupling and high cohesion, is the Law of Demeter, which is also about loose coupling.

Here's how these principles would shift the way you think about the execution flow and the relationship of your different program elements. The following may seem a bit advanced to you but try to read through it anyway and see if it makes sense. Don't worry if it's too much for you to take all in right now; you could probably get by just fixing whatever immediate problems you have with your current code. I'm just giving this as something you can mull over when you have some extra time and really want to improve your programming.

The UI, the Players, and the Game are some of the different "entities" in this program. Each of these entities has its own set of responsibilities and would have certain things that only it will know about and do. Your main method should just set the stage for the whole "story" to take place. It would create a Game, as many Players as necessary, and initialize the UI. It would tell each program entity about the other entities that they can interact with. If you think about these entities as sentient things, your "conversation" with them would go something like this:

You: "Hey, Game, this is the user and this is the dealer who are playing you. And this is the deck that you're going to use, Ok?"

In code, that would be represented as:

Game: "Ok." (no exceptions or errors thrown)

Then, you'd set up a UI and tell it what Game is being played.

You: "Hey, UI, this is the game that's currently being played, Ok?"

UI: "Ok." (no exceptions or errors thrown)

And then, you'd "wire up" your GamePanel so that any user interactions that it listens for, such as pressing a button or entering text into a dialog box, would activate behaviors in theGame that it is currently working with. That is, you'd set up the various "listeners" in the UI and have the event handlers call different methods of the current Game. For example, if the user clicks on the "Hit" button, you'd call game.hit() in the event handler method. The UI class is also where you would have all those calls to JOptionPane methods. After you do the "wiring up" for every event handler in the UI to trigger appropriate behaviors in the Game, all you have to do is "raise the curtain" with something like:

And you're off to the races! From that point, the program simply responds with actions that the user triggers by doing something in the user interface. That's kind of where the "we'll call you" thing of the Hollywood Principle comes into play.

This is the way you have to think when you're doing event-driven programming.

Does that make sense?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good way to organize your thoughts for event-driven programs is to think in terms of events, entities, and actions.

1. Event: User clicks on "Hit" button. Entities involved: UI, Game. Actions: UI listens for the button click event and calls the "hit()" method of the Game.
2. Event: Game.hit() is activated. Entities involved: Game, Dealer, Player, Deck. Actions: Game checks whose turn it is. Game tells the player whose turn it is to take a card from the Deck... and so on.
3. Event: Game.hit() results in the dealer going bust. Entities involved: ... Actions: ...
and so on

This way, you can break the program down into neat little "chunks" of behavior and tackle each one in turn.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!