0

There is source code which is not allowed to be edited and I need to add an aspect class that modifies it slightly.

Here is the class that I made:

package ext;

import java.awt.event.ActionEvent;
import javax.swing.*;
import battleship.*;

 public privileged aspect AddStrategy {


    JButton playButton1 = new JButton("Play");

    after(BattleshipDialog dialog): this(dialog) && execution(JPanel BattleshipDialog.makeControlPane()){

        dialog.playButton.setText("Practice");
        JPanel buttons = (JPanel) dialog.playButton.getParent();
        buttons.add(playButton1);
        playButton1.setFocusPainted(false);
        playButton1.addActionListener(this::playButton1Clicked);
    }

    BattleshipDialog bd;
    public void playButton1Clicked(ActionEvent event) {
        bd.startNewGame();
    }
}

I was able to rename the original button "Play" to "Practice" successfully. Whenever I click the "Practice" button, it has the original functionality of restarting the game and works fine.

However, when I try to mimic that functionality with the newly created button "Play" the console shows a bunch of errors all saying blahblahblah(UnknownSource).

I think I'm getting this issue because I need to be augmenting the same "dialog" and I'm trying to use "dialog" and "bd." However, I tried various ways to make them use the same dialog and none of them worked.

tl;dr Trying to get 2 buttons to do the same thing, but only 1 button does it while the other button throws a bunch of "Unknown Source" errors to the console.

I'm not sure if it will help, but here's the relevant source code from the BattleshipDialog.java class:

public void playButtonClicked(ActionEvent event) {
    if (isGameOver()) {
        startNewGame();
    } else {
        if (JOptionPane.showConfirmDialog(BattleshipDialog.this, 
            "Play a new game?", "Battleship", JOptionPane.YES_NO_OPTION)
            == JOptionPane.YES_OPTION) {
            startNewGame();
        }
    }
}

/** Start a new game. This will terminate the current play and
 * start a new play. */
private void startNewGame() {
    msgBar.setText("Shots: 0");
    board.reset();
    placeShips();
    repaint();
}

EDIT: Here are the error messages as per requested:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ext.AddStrategy.playButton1Clicked(AddStrategy.aj:30)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Muhd
  • 24,305
  • 22
  • 61
  • 78
Omar Khalik
  • 13
  • 1
  • 1
  • 9
  • please provide that `blahblahblah(UnknownSource)` message dear –  Apr 17 '17 at 23:25
  • Added them now :) – Omar Khalik Apr 17 '17 at 23:27
  • error because of `bd.startNewGame();`, the `bd` is null, initialize it :D –  Apr 17 '17 at 23:33
  • Thanks, I tried to initialize it but I wasn't able to. What do I initialize it to? – Omar Khalik Apr 17 '17 at 23:36
  • 1
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 17 '17 at 23:39
  • Hey Thomas, I edited the OP a little bit. I understand Null Pointer Exceptions in their most basic forms. I'm confused in this instance because I am trying to use some functionality from outside classes, some of which are declared private, and attempting to use it in this Aspect class I'm making. If I stick to only using one instance of BattleshipDialog, it then breaks the functionality of what was previously working. – Omar Khalik Apr 17 '17 at 23:48
  • @OmarKhalik I cannot tell you how to initialize the `bd` dear, you need to find it out. but the error you got is about the null point. declare the `bd` first, then make the private methods you want to call to public –  Apr 18 '17 at 00:05
  • I wish it were so easy :/ we aren't allowed to change the source code whatsoever, including changing any methods in them to public. Thank you for your help though, I now know to focus on figuring out how to initialize bd – Omar Khalik Apr 18 '17 at 00:09
  • I don't get your question. bd is not initialized, so you get an npe. So you have to initialize that thing before that method is called. In that sense it seems that you do not understand what an npe means... – GhostCat Apr 18 '17 at 03:31
  • Hi Ghost, I found the problem. I had to initialize bd inside of the aspect for it to work. I tried initializing it outside of the aspect, and it wouldn't work because I couldn't reuse dialog and so bd was a completely separate variable from dialog and I needed them to be the same. Thanks for your input – Omar Khalik Apr 18 '17 at 03:42

0 Answers0