Results 1 to 16 of 16
  1. #1
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default java.lang.NullPointerException and i don't know why

    Hi guys....

    I have started programming with java since a few months now. First the basis stats with hellow world in the old fashioned way with System.out.println :)

    Now I want to build a desktop application, quite simple still (I guess).

    I have created a new desktop application with netbeans, so I have allready a GUI.
    In this GUI I have added 2 thing.

    A jLabel and a jButton

    I have assigned an actionPerformed on the jButton.

    Below a small piece of the source of DesktopView:

    DesktopView.class
    public class DesktopView extends FrameView {
    public Reset reset;

    public DesktopView(SingleFrameApplication app) {
    super(app);

    initComponents();

    //some more code should be between this


    private void enableButtonActionPerformed(java.awt.event.ActionE vent evt) {
    reset.test();
    }
    }


    Okay the class Reset which I have created is not that complex. I want this class to enable the label in the GUI.

    Reset.class
    public class Reset {
    public DesktopView desk;

    public void test(){
    System.out.println("test"); //to check if the button works
    desk.foLabel.setEnabled(true);
    }
    }


    But here it comes, when I click the enable button it gives me an exception.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at desktop.DesktopView.generateDTPButtonActionPerform ed(DesktopView.java:1011)
    at desktop.DesktopView.access$1600(DesktopView.java:3 5)
    at desktop.DesktopView$12.actionPerformed(DesktopView .java:645)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)


    When I create the same kind of method in the DesktopView it works correctly.
    So my guess is that the classes don't communicate with each other.

    How can I fix this?


    Cheers,
    Qubes
    Last edited by Qubes; 01-12-2011 at 11:25 PM. Reason: Classes added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm guessing that your null exception occurs here:
    Java Code:
    desk.foLabel.setEnabled(true);
    And it would make sense to me since the desk variable never is initialized to anything, or in other words is a null variable. Just because it has the same name as the variable in your other class will not magically make it refer to the same object. You have to do this yourself either through the Reset class constructor or through a setDesk(DesktopView desk) method that you give it and call.

  3. #3
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Hi Fubarable,

    that was my first thought too that at the point went something wrong.
    However it still crash if i change that row to a comment line

    I thought a constructor should be like this:

    public class Reset {
    public DesktopView desk;

    public Reset(DesktopView desktop){
    this.desk=desktop;
    }

    public void test(){
    System.out.println("test");
    //desk.foLabel.setEnabled(true);
    }
    }

    but that didn't even work.

    Sorry for being a noob in Java.

  4. #4
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    It tells you the line number where it is occurring.
    DesktopView.java:1011

  5. #5
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by hosscomp View Post
    It tells you the line number where it is occurring.
    That's my enable button...

    called it first generateDTP



    However...
    If i have the method in the DesktopView class it works fine.
    In the DesktopView class I have declared
    Java Code:
    private Reset reset;
    so I can access the public methods in that class with reset.<methodname>


    This doesn't work correctly right now.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    The whole DesktopView class isn't posted so the stack trace is fairly meaningless to us...recommend posting a SSCCE. Based upon the snippet..is reset ever initialized in the DesktopView class?

  7. #7
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Did you initialize reset?
    Java Code:
    _private Reset reset = new Reset();
    It is hard to guess what your code looks like.

  8. #8
    Tomtefaen is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Have you actually declared an instance of reset?

    Right before the button where you call reset.test(), try setting reset=new Reset(this);

  9. #9
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    The whole DesktopView class isn't posted so the stack trace is fairly meaningless to us...recommend posting a SSCCE. Based upon the snippet..is reset ever initialized in the DesktopView class?
    I didn't do that because there is much more code behind it.

    Around 1100 rows right now.
    I am trying to split some functionalities to other classes.

    Edit
    I will create a smaller piece of it today when I am back from work
    Last edited by Qubes; 01-13-2011 at 08:31 AM.

  10. #10
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Okaye I have created a similar application just like what i have


    Reset class

    Java Code:
    public class Reset {
    
        private TestProjectView desk;
    
        public void enable(){
            desk.enableLabel.setEnabled(true);
        }
    
    }


    TestProjectView

    Java Code:
    import org.jdesktop.application.Action;
    import org.jdesktop.application.ResourceMap;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    import org.jdesktop.application.TaskMonitor;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    
    /**
     * The application's main frame.
     */
    public class TestProjectView extends FrameView {
        private Reset reset;
    
        public TestProjectView(SingleFrameApplication app) {
            super(app);
    
            initComponents();
    
            // status bar initialization - message timeout, idle icon and busy animation, etc
            ResourceMap resourceMap = getResourceMap();
            int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
            messageTimer = new Timer(messageTimeout, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    statusMessageLabel.setText("");
                }
            });
            messageTimer.setRepeats(false);
            int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
            for (int i = 0; i < busyIcons.length; i++) {
                busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
            }
            busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                    statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
                }
            });
            idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
            statusAnimationLabel.setIcon(idleIcon);
            progressBar.setVisible(false);
    
            // connecting action tasks to status bar via TaskMonitor
            TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
            taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    String propertyName = evt.getPropertyName();
                    if ("started".equals(propertyName)) {
                        if (!busyIconTimer.isRunning()) {
                            statusAnimationLabel.setIcon(busyIcons[0]);
                            busyIconIndex = 0;
                            busyIconTimer.start();
                        }
                        progressBar.setVisible(true);
                        progressBar.setIndeterminate(true);
                    } else if ("done".equals(propertyName)) {
                        busyIconTimer.stop();
                        statusAnimationLabel.setIcon(idleIcon);
                        progressBar.setVisible(false);
                        progressBar.setValue(0);
                    } else if ("message".equals(propertyName)) {
                        String text = (String)(evt.getNewValue());
                        statusMessageLabel.setText((text == null) ? "" : text);
                        messageTimer.restart();
                    } else if ("progress".equals(propertyName)) {
                        int value = (Integer)(evt.getNewValue());
                        progressBar.setVisible(true);
                        progressBar.setIndeterminate(false);
                        progressBar.setValue(value);
                    }
                }
            });
        }
    
        @Action
        public void showAboutBox() {
            if (aboutBox == null) {
                JFrame mainFrame = TestProjectApp.getApplication().getMainFrame();
                aboutBox = new TestProjectAboutBox(mainFrame);
                aboutBox.setLocationRelativeTo(mainFrame);
            }
            TestProjectApp.getApplication().show(aboutBox);
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            mainPanel = new javax.swing.JPanel();
            enableLabel = new javax.swing.JLabel();
            enableButton = new javax.swing.JButton();
            menuBar = new javax.swing.JMenuBar();
            javax.swing.JMenu fileMenu = new javax.swing.JMenu();
            javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
            javax.swing.JMenu helpMenu = new javax.swing.JMenu();
            javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
            statusPanel = new javax.swing.JPanel();
            javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
            statusMessageLabel = new javax.swing.JLabel();
            statusAnimationLabel = new javax.swing.JLabel();
            progressBar = new javax.swing.JProgressBar();
    
            mainPanel.setName("mainPanel"); // NOI18N
    
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testproject.TestProjectApp.class).getContext().getResourceMap(TestProjectView.class);
            enableLabel.setText(resourceMap.getString("enableLabel.text")); // NOI18N
            enableLabel.setEnabled(false);
            enableLabel.setName("enableLabel"); // NOI18N
    
            enableButton.setText(resourceMap.getString("enableButton.text")); // NOI18N
            enableButton.setName("enableButton"); // NOI18N
            enableButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    enableButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
            mainPanel.setLayout(mainPanelLayout);
            mainPanelLayout.setHorizontalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(enableLabel)
                        .addComponent(enableButton))
                    .addContainerGap(275, Short.MAX_VALUE))
            );
            mainPanelLayout.setVerticalGroup(
                mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(enableLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(enableButton)
                    .addContainerGap(200, Short.MAX_VALUE))
            );
    
            menuBar.setName("menuBar"); // NOI18N
    
            fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
            fileMenu.setName("fileMenu"); // NOI18N
    
            javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(testproject.TestProjectApp.class).getContext().getActionMap(TestProjectView.class, this);
            exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
            exitMenuItem.setName("exitMenuItem"); // NOI18N
            fileMenu.add(exitMenuItem);
    
            menuBar.add(fileMenu);
    
            helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
            helpMenu.setName("helpMenu"); // NOI18N
    
            aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
            aboutMenuItem.setName("aboutMenuItem"); // NOI18N
            helpMenu.add(aboutMenuItem);
    
            menuBar.add(helpMenu);
    
            statusPanel.setName("statusPanel"); // NOI18N
    
            statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
    
            statusMessageLabel.setName("statusMessageLabel"); // NOI18N
    
            statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
            statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
    
            progressBar.setName("progressBar"); // NOI18N
    
            javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
            statusPanel.setLayout(statusPanelLayout);
            statusPanelLayout.setHorizontalGroup(
                statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(statusMessageLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(statusAnimationLabel)
                    .addContainerGap())
            );
            statusPanelLayout.setVerticalGroup(
                statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(statusPanelLayout.createSequentialGroup()
                    .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(statusMessageLabel)
                        .addComponent(statusAnimationLabel)
                        .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(3, 3, 3))
            );
    
            setComponent(mainPanel);
            setMenuBar(menuBar);
            setStatusBar(statusPanel);
        }// </editor-fold>
    
        private void enableButtonActionPerformed(java.awt.event.ActionEvent evt) {
            reset.enable();
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton enableButton;
        public javax.swing.JLabel enableLabel;
        private javax.swing.JPanel mainPanel;
        private javax.swing.JMenuBar menuBar;
        private javax.swing.JProgressBar progressBar;
        private javax.swing.JLabel statusAnimationLabel;
        private javax.swing.JLabel statusMessageLabel;
        private javax.swing.JPanel statusPanel;
        // End of variables declaration
    
        private final Timer messageTimer;
        private final Timer busyIconTimer;
        private final Icon idleIcon;
        private final Icon[] busyIcons = new Icon[15];
        private int busyIconIndex = 0;
    
        private JDialog aboutBox;
    }
    TestProjectAboutBox
    Java Code:
    import org.jdesktop.application.Action;
    
    public class TestProjectAboutBox extends javax.swing.JDialog {
    
        public TestProjectAboutBox(java.awt.Frame parent) {
            super(parent);
            initComponents();
            getRootPane().setDefaultButton(closeButton);
        }
    
        @Action public void closeAboutBox() {
            dispose();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            closeButton = new javax.swing.JButton();
            javax.swing.JLabel appTitleLabel = new javax.swing.JLabel();
            javax.swing.JLabel versionLabel = new javax.swing.JLabel();
            javax.swing.JLabel appVersionLabel = new javax.swing.JLabel();
            javax.swing.JLabel vendorLabel = new javax.swing.JLabel();
            javax.swing.JLabel appVendorLabel = new javax.swing.JLabel();
            javax.swing.JLabel homepageLabel = new javax.swing.JLabel();
            javax.swing.JLabel appHomepageLabel = new javax.swing.JLabel();
            javax.swing.JLabel appDescLabel = new javax.swing.JLabel();
            javax.swing.JLabel imageLabel = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(testproject.TestProjectApp.class).getContext().getResourceMap(TestProjectAboutBox.class);
            setTitle(resourceMap.getString("title")); // NOI18N
            setModal(true);
            setName("aboutBox"); // NOI18N
            setResizable(false);
    
            javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(testproject.TestProjectApp.class).getContext().getActionMap(TestProjectAboutBox.class, this);
            closeButton.setAction(actionMap.get("closeAboutBox")); // NOI18N
            closeButton.setName("closeButton"); // NOI18N
    
            appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD, appTitleLabel.getFont().getSize()+4));
            appTitleLabel.setText(resourceMap.getString("Application.title")); // NOI18N
            appTitleLabel.setName("appTitleLabel"); // NOI18N
    
            versionLabel.setFont(versionLabel.getFont().deriveFont(versionLabel.getFont().getStyle() | java.awt.Font.BOLD));
            versionLabel.setText(resourceMap.getString("versionLabel.text")); // NOI18N
            versionLabel.setName("versionLabel"); // NOI18N
    
            appVersionLabel.setText(resourceMap.getString("Application.version")); // NOI18N
            appVersionLabel.setName("appVersionLabel"); // NOI18N
    
            vendorLabel.setFont(vendorLabel.getFont().deriveFont(vendorLabel.getFont().getStyle() | java.awt.Font.BOLD));
            vendorLabel.setText(resourceMap.getString("vendorLabel.text")); // NOI18N
            vendorLabel.setName("vendorLabel"); // NOI18N
    
            appVendorLabel.setText(resourceMap.getString("Application.vendor")); // NOI18N
            appVendorLabel.setName("appVendorLabel"); // NOI18N
    
            homepageLabel.setFont(homepageLabel.getFont().deriveFont(homepageLabel.getFont().getStyle() | java.awt.Font.BOLD));
            homepageLabel.setText(resourceMap.getString("homepageLabel.text")); // NOI18N
            homepageLabel.setName("homepageLabel"); // NOI18N
    
            appHomepageLabel.setText(resourceMap.getString("Application.homepage")); // NOI18N
            appHomepageLabel.setName("appHomepageLabel"); // NOI18N
    
            appDescLabel.setText(resourceMap.getString("appDescLabel.text")); // NOI18N
            appDescLabel.setName("appDescLabel"); // NOI18N
    
            imageLabel.setIcon(resourceMap.getIcon("imageLabel.icon")); // NOI18N
            imageLabel.setName("imageLabel"); // NOI18N
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(imageLabel)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(versionLabel)
                                .addComponent(vendorLabel)
                                .addComponent(homepageLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(appVersionLabel)
                                .addComponent(appVendorLabel)
                                .addComponent(appHomepageLabel)))
                        .addComponent(appTitleLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(appDescLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                        .addComponent(closeButton))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(appTitleLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(appDescLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(versionLabel)
                        .addComponent(appVersionLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(vendorLabel)
                        .addComponent(appVendorLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(homepageLabel)
                        .addComponent(appHomepageLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                    .addComponent(closeButton)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
        
        // Variables declaration - do not modify
        private javax.swing.JButton closeButton;
        // End of variables declaration
        
    }

    TestProjectApp
    Java Code:
    import org.jdesktop.application.Application;
    import org.jdesktop.application.SingleFrameApplication;
    
    /**
     * The main class of the application.
     */
    public class TestProjectApp extends SingleFrameApplication {
    
        /**
         * At startup create and show the main frame of the application.
         */
        @Override protected void startup() {
            show(new TestProjectView(this));
        }
    
        /**
         * This method is to initialize the specified window by injecting resources.
         * Windows shown in our application come fully initialized from the GUI
         * builder, so this additional configuration is not needed.
         */
        @Override protected void configureWindow(java.awt.Window root) {
        }
    
        /**
         * A convenient static getter for the application instance.
         * @return the instance of TestProjectApp
         */
        public static TestProjectApp getApplication() {
            return Application.getInstance(TestProjectApp.class);
        }
    
        /**
         * Main method launching the application.
         */
        public static void main(String[] args) {
            launch(TestProjectApp.class, args);
        }
    }
    Error Message
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at testproject.TestProjectView.enableButtonActionPerf ormed(TestProjectView.java:218)
    at testproject.TestProjectView.access$800(TestProject View.java:22)
    at testproject.TestProjectView$4.actionPerformed(Test ProjectView.java:129)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at testproject.TestProjectView.enableButtonActionPerf ormed(TestProjectView.java:218)
    at testproject.TestProjectView.access$800(TestProject View.java:22)
    at testproject.TestProjectView$4.actionPerformed(Test ProjectView.java:129)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 9 seconds)



    I think that I forgot something like a constructor but I am not sure.

  11. #11
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    I don't have the jdesktop package so can't comple it, but I don't see the reset instance being created.
    You declare it as Private Reset reset but never create it. Try:
    Java Code:
    public class TestProjectView extends FrameView {
        private Reset reset = new Reset();
    But looking at the Reset class I don't see how it can work.
    You declare a private TestProjectView but never create it, so it is also null
    unless I am missing something.
    Java Code:
    public class Reset {
    
        private TestProjectView desk;
    
        public void enable(){
            desk.enableLabel.setEnabled(true);
        }
    
    }

  12. #12
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    public class TestProjectView extends FrameView {
        private Reset reset = new Reset();
    Didn't work for me:(

    You are correct in the reset one...

    see error
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at testproject.Reset.enable(Reset.java:17)

    How can I make sure that this won't be a null anymore?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  14. #14
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    solved :)


    Thanks had to make a constructor with arguments


    Java Code:
    public class TestProjectView extends FrameView {
        private Reset reset = new Reset(this);

    Java Code:
    public class Reset {
    
        public TestProjectView desk ;
    
        public Reset(TestProjectView desktop){
            this.desk=desktop;
        }
    
        public void enable(){
            desk.enableLabel.setEnabled(true);
        }
    
    }

  15. #15
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    You are correct in the reset one...

    see error
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at testproject.Reset.enable(Reset.java:17)

    How can I make sure that this won't be a null anymore?
    You are probably creating only one instance of TestProjectView and that is
    the one you want to access from Reset. Why not put a public reset method in the class
    that does a
    Java Code:
    this.enableLabel.setEnabled(true);
    then
    Java Code:
        private void enableButtonActionPerformed(java.awt.event.ActionEvent evt) {
            this.reset();
        }
    Or just
    Java Code:
        private void enableButtonActionPerformed(java.awt.event.ActionEvent evt) {
            this.enableLabel.setEnabled(true);
        }
    Edit:
    Oops. Didn't see that you had already solved it.
    Last edited by hosscomp; 01-13-2011 at 07:24 PM.

  16. #16
    Qubes is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, where are you setting desk to not be null?
    Quote Originally Posted by hosscomp View Post
    You are probably creating only one instance of TestProjectView and that is
    the one you want to access from Reset. Why not put a public reset method in the class
    that does a
    Java Code:
    this.enableLabel.setEnabled(true);
    then
    Java Code:
        private void enableButtonActionPerformed(java.awt.event.ActionEvent evt) {
            this.reset();
        }
    Or just
    Java Code:
        private void enableButtonActionPerformed(java.awt.event.ActionEvent evt) {
            this.enableLabel.setEnabled(true);
        }
    Edit:
    Oops. Didn't see that you had already solved it.
    Thanks for your time.


    Why not?

    Because I all ready did and the code in the class was not that great when you want to adjust some code.

    That's why I want to split some functionality to other classes so I can find faster bugs or adjust the code.

Similar Threads

  1. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 04:12 PM
  2. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 08:15 PM
  3. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 06:49 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 01:36 PM
  5. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •