PDA

View Full Version : Having trouble calling method in seperate class....New guy


FlyingRat
12-29-2009, 08:11 AM
I'm trying to create a simple app that will open a browser window based on a value placed into a textfield. The app will eventually do much more, but I'm just beginning, and I'm new to this.

I can get this to occur successfully in I just create one class. If I add a second class to the package, and transfer what I think to be all the appropriate code, I get a bunch of errors thrown.

Here's the code for the main class (check the urlButtonActionPerformed method):

/*
* MainScreen.java
*
* Created on Dec 28, 2009, 6:52:03 PM
*/

package irStats_Trigger;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
*
* @author phil
*/
public class MainScreen extends javax.swing.JFrame {

/** Creates new form MainScreen */
public MainScreen() {
initComponents();
//disable all buttons and fields until verified capable
disableComponents();
//enable supported options
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
// now enable buttons for actions that are supported.
enableSupportedActions();
}
}

private void disableComponents() {
urlButton.setEnabled(false);
urlField.setEnabled(false);
}

private void enableSupportedActions() {
if (desktop.isSupported(Desktop.Action.BROWSE)) {
urlButton.setEnabled(true);
urlField.setEnabled(true);
}
}

private void openURI(URI u) {
URI uri = null;
try {
uri = new URI("http://www.irstats.com");
desktop.browse(uri);
}
catch(IOException ioe) {
//ioe.printStackTrace();
}
catch(URISyntaxException use) {
//System.out.println("Problem in OpenURI");
//use.printStackTrace();
}
}

/** 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() {

urlField = new javax.swing.JTextField();
urlButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("irStats Timing Trigger");

urlField.setText("http://");

urlButton.setText("URL");
urlButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
urlButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(urlField, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(urlButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(urlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(urlButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void urlButtonActionPerformed(java.awt.event.ActionEvent evt) {
URI uri = null;

try {
uri = new URI(urlField.getText());
//openURI(uri); ***This will work
//desktop.browse(uri); **This will Work
Browsing b = new Browsing(); //This works fine
b.openURI(uri); //This doesn't work
}
catch(IOException ioe){

}
catch(URISyntaxException use) {
//System.out.println("in Main");
//use.printStackTrace();
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainScreen().setVisible(true);
}
});
}

// Personal variables declaration - add additional here
private Desktop desktop;
private Desktop.Action action;
// End Personal variables declaration

// Variables declaration - do not modify
private javax.swing.JButton urlButton;
private javax.swing.JTextField urlField;
// End of variables declaration

}


Here is the code for the Browsing class:

/**
* This class accepts information that will ultimately open the default
* browser with single or multiple tabs to specific URIs
*/

package irStats_Trigger;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;
import java.awt.Desktop;

/** BEGIN CLASS **/

public class Browsing {

public Browsing() {

}

public void openURI(URI uri) {
try {
desktop.browse(uri);
}
catch(IOException ioe) {
//ioe.printStackTrace();
}
}


// Personal variables declaration - add additional here
private Desktop desktop;
private Desktop.Action action;
// End Personal variables declaration

}


Here are the exceptions being thrown:


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at irStats_Trigger.Browsing.openURI(Browsing.java:22)
at irStats_Trigger.MainScreen.urlButtonActionPerformed(MainScreen.java:116)
at irStats_Trigger.MainScreen.access$000(MainScreen.java:18)
at irStats_Trigger.MainScreen$1.actionPerformed(MainScreen.java:80)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


I've even tried hardcoding a URI into the Browsing class with no luck. I know that this must be a very simple thing I just don't fully understand yet. Thanks for the help.

Sorry if I didn't need to include ALL the code, I just didn't want anything left out that may have needed to be included.

cs_student
12-29-2009, 07:51 PM
i didn't look through all of your code but it looks as though the problem creating the error is in the method
public void openURI(URI uri) {
try {
desktop.browse(uri);
}
catch(IOException ioe) {
//ioe.printStackTrace();
}
}

You have declared a variable desktop to be of type Desktop. However you have not initialized this variable thus it points to nothing (hence the NullPointer exception).

I have a feeling that you are trying to access the desktop variable from your other class. Maybe pass that in the openURI method as a second parameter or pass it into the constructor of your Browse class and initialize the Browse.desktop variable to the point to the desktop object which the other references.


cs_student