Java Desktop Application Fehler

PEASANT KING

Commander
Registriert
Okt. 2008
Beiträge
2.412
Hallo Leute,

ich bin es mal wieder.
Ich habe eine simple Desktop Application geschrieben und stehe nun vor einem Problem und zwar erhalte ich immer eine NullPointerException die eigentlich nach meinem Wissen nicht auftreten dürfte.

Hier mal der Code:
PHP:
/*
 * SKSJBusiness2011App.java
 */

package sksjbusiness2011;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class SKSJBusiness2011App extends SingleFrameApplication {

    private SKSJBusiness2011Login login;
    
    @Override protected void startup() {
        show(new SKSJBusiness2011Login(login));

        if(login.loginApp()== true){   
            show(new SKSJBusiness2011View(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 SKSJBusiness2011App
     */
    public static SKSJBusiness2011App getApplication() {
        return Application.getInstance(SKSJBusiness2011App.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(SKSJBusiness2011App.class, args);
    }
}
Die Exception tritt bei der IF Abfrage auf in der Methode startup()

Die zusammenhengende Klasse Login mit Code:
PHP:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SKSJBusiness2011Login.java
 *
 * Created on 07.06.2011, 11:10:17
 */
package sksjbusiness2011;

import java.awt.SplashScreen;
import org.jdesktop.application.Action;

/**
 *
 * @author ll
 */
public class SKSJBusiness2011Login extends javax.swing.JDialog {

    /** Creates new form SKSJBusiness2011Login */
    public SKSJBusiness2011Login(java.awt.Dialog parent) {
        super(parent);
        initComponents();
    }
    
    @Action public boolean loginApp(){    
        
        if(jTextField1.getText().equals("Admin")){   
        }
        return true;
    }
        
    @Action public void closeLoginBox() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sksjbusiness2011.SKSJBusiness2011App.class).getContext().getResourceMap(SKSJBusiness2011Login.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(sksjbusiness2011.SKSJBusiness2011App.class).getContext().getActionMap(SKSJBusiness2011Login.class, this);
        jButton1.setAction(actionMap.get("loginApp")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("closeLoginBox")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jPasswordField1.setText(resourceMap.getString("jPasswordField1.text")); // NOI18N
        jPasswordField1.setName("jPasswordField1"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // 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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPasswordField1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
 
bitte poste den die StackTrace-Ausgabe!

EDIT: Da ich mit jdesktop nicht vertraut bin mal eine Kurze Frage:
Wer instanziert ein login-Objekt in deinem Code (SKSJBusiness2011Login) bevor startup aufgerufen wird?

Ich sehe nur die Instanzierung in "startup" selber, aber das Objekt wird ja nur show übergeben und nicht auf login zugewiesen.
 
Zuletzt bearbeitet:
Code:
07.06.2011 14:42:39 org.jdesktop.application.Application$1 run
SCHWERWIEGEND: Application class sksjbusiness2011.SKSJBusiness2011App failed to launch
java.lang.NullPointerException
    at sksjbusiness2011.SKSJBusiness2011App.startup(SKSJBusiness2011App.java:20)
    at org.jdesktop.application.Application$1.run(Application.java:171)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Application class sksjbusiness2011.SKSJBusiness2011App failed to launch
    at org.jdesktop.application.Application$1.run(Application.java:177)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    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)
Caused by: java.lang.NullPointerException
    at sksjbusiness2011.SKSJBusiness2011App.startup(SKSJBusiness2011App.java:20)
    at org.jdesktop.application.Application$1.run(Application.java:171)
    ... 8 more
 
Siehe Frage in meinem Edit meiner 1. Antwort!
 
Ich habe noch eine Klasse View die alles instanziert bis auf das Login...

EDIT:

Sorry Frage falsch verstanden ich glaube was du wissen willst passiert in:
PHP:
    public SKSJBusiness2011Login(java.awt.Dialog parent) {
        super(parent);
        initComponents();
    }
mit der Methode initComponents()
 
Zuletzt bearbeitet:
Wer Instanziert SKSJBusiness2011Login und weist es dem login-Objekt in SKSJBusiness2011App zu?

Ich verstehe das
bis auf das Login
nicht so ganz, denn d.h. es ist immer null da nie instanziert ... :confused_alt:
 
Ahh okay ich verstehe.
Ich muss dazu sagen ich bin jetzt nicht gerade der Meister von Java, aber instanziert wird es durch:
Code:
private SKSJBusiness2011Login login;

Der Login Dialog wird ja auch angezeigt nur wenn ich dann den Benutzernamen eingebe und auf Anmelden klicke sollte ja die eigentliche Applikation aufgerufen werden, was nicht geschieht wegen der NullPointerException.
 
Code:
private SKSJBusiness2011Login login;

ist keine Instantiierung, sondern eine Deklaration... Eigentlich sollte dir das ein guter Java Editor auch 'anmalen', da du das Objekt nicht vor seiner eigentlichen Erzeugung/Instantiierung benutzen darfst (Ausnahme: Singletons, statische Objekte)...

Richtig sollte das in etwa so aussehen:
Code:
private SKSJBusiness2011Login login = new SKSJBusiness2011Login(.......);

....... ersetzen durch eventuelle Argumente...
Oder du erzeugst das Objekt im Konstruktor:
Code:
login = new SKSJBusiness2011Login(.......);
 
Zuletzt bearbeitet:
DJ_We$t schrieb:
...
Der Login Dialog wird ja auch angezeigt ...

Ist auch klar da du show eine Instanz übergibst (mit new erzeugst du ein Objekt)

Code:
show(new SKSJBusiness2011View(this));

besser wäre wie blah schon gemeint hat:

private SKSJBusiness2011Login login = new SKSJBusiness2011Login(.......);

und dann im startup

Code:
show(this.login);
 
Gut wenn ich es so instanziere wie oben von Blah erwähnt dann funktioniert garnichts.
Ich benutze ausserdem das Swing Application Framework, daher show();

Ich kann euch ja mal den Sourcecode uploaden.

PS: Irgendwie werd ich wohl nie aus Java schlau -.-
 
Zuletzt bearbeitet:
was bedeutet garnichts. poste mal deine Exceptions ;)

BTW: Ich nutze keine jdesktop-Klassen (scheint was aus dem Netbeans IDE zu sein) soviel ich sehe.

EDIT:

Ändere mal folgendes in SKSJBusiness2011App.java:

private SKSJBusiness2011Login login;

zu
private SKSJBusiness2011Login login = new SKSJBusiness2011Login( null );

und

show(new SKSJBusiness2011Login(login));

zu

show( this.login );
 
Zuletzt bearbeitet:
PHP:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class java.util.Formatter
    at java.lang.String.format(String.java:2769)
    at org.jdesktop.application.Application$1.run(Application.java:175)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    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)
BUILD SUCCESSFUL (total time: 2 seconds)
Jap Netbeans IDE das ist Korrekt

EDIT2:

Gleicher Fehler was mich aber nicht sonderlich verwundert, ich im Konstruktor ja nicht (null) übergeben kann sondern das es sich um ein awt.Dialog handelt.
 
Zuletzt bearbeitet:
Die Klasse java.util.Formatter wird in java.lang.String.format verwendet, das hat jetzt nix mit deinem Code zu tun.

Sehr seltsam das ganze, sieht irgendwie so aus ob ältere JDK-Klassen (Jar's vor Java 1.5) mit neueren gemischt sind.
 
Wie kann das denn sein, ich dachte das wird mit Netbeans abgedeckt.
Oder sehe ich das falsch, habe Netbeans 7.0 installiert.
 
poste bitte nochmal deine "aktuelle Implementierung" der startup-methode, prüf mal ob du die Änderungen so durchgeführt hast wie ich gemeint habe!
 
Muss ich später machen, muss hier eine Schnittstelle testen.
 
Am besten du setzt -verbose als Schalter in Netbeans beim Test damit sieht man evtl. wichtige Details.

Evtl. gibts ist eine andere Exception der Auslöser und ist dann als Cause zu sehen!

BTW: bin bis 8:00 offline! ;)
 
Zuletzt bearbeitet:
Wo setz ich genau -verbose ?

Um nochmal zu erleutern was ich machen will...

Der Login Dialog soll vor der eigentlichen Applikation gezeigt werden, wenn ich nun ein korrektes PW und Benutzername eingebe und auf anmelden klicke soll der Logindialog verschwinden und die eigentliche App gestartet werden.
 
Zuletzt bearbeitet:
Ich nutze Netbeans nicht, aber soweit ich mich erinnere kann man unter "Project Properties" "Run" unter "Arguments" als Wert
eintragen, damit sollten detailiertere Ausgaben (evtl. caused by) zu sehen sein.

Alternativ kannst du das auch über die Konsole
java -verbose <Java-HauptKlasse>
 
Also wenn ich mir das so anschaue in meiner Originalversion gibt meine IF Abfrage eine NullpointerException aus.
Warum ist das so ? Es kann doch nicht sein.
Oder kann ich nicht Abfragen ob das Ergebnis der Funktion loginApp() true oder false ist ?
Ich meine die Funktion returned ja auch entweder true oder false...

Hab das nun mal umgeschrieben:
PHP:
/*
 * SKSJBusiness2011App.java
 */

package sksjbusiness2011;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class SKSJBusiness2011App extends SingleFrameApplication {
    
    private SKSJBusiness2011Login login;
    private SKSJBusiness2011Login loginMethod;
    private java.awt.Dialog loginDialog;
   
    @Override protected void startup() {
        show(new SKSJBusiness2011Login(login)); 
        loginMethod = new SKSJBusiness2011Login(loginDialog);
        
        if(loginMethod.loginApp() == true){
            show(new SKSJBusiness2011View(this)); 
        
        }
    }
    /*
    private SKSJBusiness2011Login login = new SKSJBusiness2011Login( null ); 
    
    @Override protected void startup() {
    show(new SKSJBusiness2011Login(login));     
        
        show(new SKSJBusiness2011View(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 SKSJBusiness2011App
     */
    public static SKSJBusiness2011App getApplication() {
        return Application.getInstance(SKSJBusiness2011App.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(SKSJBusiness2011App.class, args);
    }
}

Allerdings bekomm ich keinen return bzw macht die If Abfrage garnichts...
 
Zuletzt bearbeitet:
Zurück
Oben