JavaFX NullPointerException bei Versuch ImageView zu ändern

Syrghar

Mitglied
Beim Versuch das Image in einer ImageView zu ändern bekomme och folgende Fehlermeldung.

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at at.fhj.swd2.project.berdievs14bergerru14.Hand.getCard(Hand.java:92)
at at.fhj.swd2.project.berdievs14bergerru14.Control$2.handle(Control.java:108)
at at.fhj.swd2.project.berdievs14bergerru14.Control$2.handle(Control.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$182/941730105.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Das hier ist besagte Methode, wo anscheinend der Fehler auftritt. getCard(); - Zeile h_imgView_main.setImage(img_test);
Java:
package at.fhj.swd2.project.berdievs14bergerru14;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Hand extends Container
{
    private String playerCard = "";
    //private String handCard;
    private int sum;

    private  ImageView h_imgView_main = GameView.getImgView_sec();
  
    private boolean firstTime = true; // Wenn Spieler einsteigt kriegt automatisch eine Karte
    public boolean isFirstTime()
    {
        return firstTime;
    }
    public void setFirstTime(boolean firstTime)
    {
        this.firstTime = firstTime;
    }
  
  
    public String getPlayerCard()
    {
        return playerCard;
    }
    public void setPlayerCard(String playerCard)
    {
        this.playerCard = playerCard;
    }
    public int getSum()
    {
        return sum;
    }
    public void setSum(int sum)
    {
        this.sum = sum;
    }
  
    public Hand()
    {
  
    }
  
 
    public void getCard() throws IOException
    {

            throwCard();
          
          
            if (value == 11) // As Karte
            {
                if (sum >= 11)
                {
                    value = 1; // Vorteil für den Spieler
                }
            }
                              
            sum = sum + value; // Summieren Kartenwertigkeit der Spieler
            playerCard = element + "|" +   playerCard; // Anzeige der Karten
              System.out.println(playerCard);
                
                        
              Card.showCard(playerCard);
                              
            // Zeichnet die letzte Karte
                              
                              
            // Paints the Card
          
                    
            
                        Image img_test = new Image("style/table.jpg", true);
                        try {
                            img_test = Card.showCardImage(playerCard);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }  
                        [B]h_imgView_main.setImage(img_test);[/B]

                        System.out.println(sum); //TODO: Übersichtlicher! Zeigt die derzeitige Gesamtsumme an
     
        }

   
    }

Aufgerufen wird die Methode in diser Control Klasse.

Java:
package at.fhj.swd2.project.berdievs14bergerru14;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;







import javax.xml.bind.JAXBException;

import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Control {
    static GameControl gControl = new GameControl();
    static GameView mainGui = new GameView();
    static Hand player_hand = new Hand();

    private static String playerCount;


    public static String getPlayerCount() {
        return playerCount;
    }

    public void startGame() throws IOException, InterruptedException, JAXBException {

        Thread thread = new Thread("NEW THREAD") {
            public void run() {

                mainGui.startView();
              
            }
        };
  
        thread.start();
      
      
        gControl.startBj();
      
        //mainGui.startView();
      
        if (playerCount == null) {
            playerCount = "0";
            System.out.println("convert ");

        }
      

    }
    List<String> testnamen = new ArrayList<String>();

    private Button c_gamer_Button = GameView.getGamer_Button();
    private Button c_gamer_plus = GameView.getGamer_plus();
    private Button c_gamer_minus = GameView.getGamer_minus();
    private Label c_top_label_gamer = GameView.getTop_label_gamer();
  
    private Label labelUsername = PlayerView.getLabelUsername();

    private Button c_button_back_main = GameView.getButton_back_main();
  
    private Button sub_button_hit = PlayerView.getSub_button_hit();
    private Button sub_button_stand = PlayerView.getSub_button_stand();

    private TextField text_player_1 = GameView.getText_player_1();
    private TextField text_player_2 = GameView.getText_player_2();
    private TextField text_player_3 = GameView.getText_player_3();
    private TextField text_player_4 = GameView.getText_player_4();
  
    private Stage c_main_stage = GameView.getMainstage();
  
    private Scene c_main_scene = GameView.getMain_scene();
    private Scene c_sec_scene = GameView.getSec_scene();
  
    public void GUIuserListeners()
    {
        sub_button_hit.setOnAction(new EventHandler<ActionEvent>() {
          
            @Override
          
            public void handle(ActionEvent aButton) {

              
                gControl.hit(labelUsername.getText());
                sub_button_hit.disableProperty();
                sub_button_hit.disabledProperty();
              
                sub_button_hit.getScene().getWindow();
                System.out.println(sub_button_hit.getScene().getWindow());
                sub_button_hit.getId();
              
              
                  
                        try {
                            [B]player_hand.getCard();[/B]
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
              
                    System.out.println("Hit gedrückt");
              
              
            }

        });
      
sub_button_stand.setOnAction(new EventHandler<ActionEvent>() {
          
            @Override
          
            public void handle(ActionEvent aButton) {


              
                System.out.println(labelUsername.getText());
                //sub_button_hit.getScene().getWindow().hide();
              
                gControl.stand(labelUsername.getText());

                PauseTransition pause = new PauseTransition(Duration.seconds(1));
              
                pause.play();
                pause.stop();
              
              
                sub_button_hit.getScene().getWindow();
              
              
            }

        });
      
    }
  
    public void GUI_Listeners() {
        // Enter number of player
      
      

      
      
c_gamer_plus.setOnAction(new EventHandler<ActionEvent>() {
          
            @Override
            public void handle(ActionEvent AE_gamer_button) {
              
                int i = Integer.parseInt(c_top_label_gamer.getText());
              
                if(i<4)
                {
                    i+=1;
                  
                    toString();
                    c_top_label_gamer.setText(String.valueOf(i));
                  
                  
      
                }
                switch(i){
                case 1:
                    text_player_2.setDisable(true);
                    text_player_3.setDisable(true);
                    text_player_4.setDisable(true);
                    break;
                case 2:
                  
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(true);
                    text_player_4.setDisable(true);
                    break;
                case 3:
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(false);
                    text_player_4.setDisable(true);
                    break;
                case 4:
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(false);
                    text_player_4.setDisable(false);
                    break;
                default:
                    break;
                }
              
            }

        });

        c_gamer_minus.setOnAction(new EventHandler<ActionEvent>() {
          
            @Override
            public void handle(ActionEvent AE_gamer_button) {
              
                int i = Integer.parseInt(c_top_label_gamer.getText());
              
                if(i>1)
                {
                    i-=1;
                  
                    toString();
                    c_top_label_gamer.setText(String.valueOf(i));
                }
                switch(i){
                case 1:
                    text_player_2.setDisable(true);
                    text_player_3.setDisable(true);
                    text_player_4.setDisable(true);
                    break;
                case 2:
                  
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(true);
                    text_player_4.setDisable(true);
                    break;
                case 3:
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(false);
                    text_player_4.setDisable(true);
                    break;
                case 4:
                    text_player_2.setDisable(false);
                    text_player_3.setDisable(false);
                    text_player_4.setDisable(false);
                    break;
                default:
                    break;
                }
              
            }

        });
      
      
        c_gamer_Button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent AE_gamer_button) {
              
                //playerCount = GameView.getGamer_list().getSelectionModel().getSelectedItem();
                playerCount = c_top_label_gamer.getText();
              
              
                System.out.println("Anzahl Spieler: " + playerCount);
              

//                for (int i =0;i < Integer.parseInt(playerCount);i++)
//                {
//                  
//                }
              
                if (text_player_1.getText()!="")
                {
                    testnamen.add(text_player_1.getText());
                }
              
                if (text_player_2.getText()!="")
                {
                    testnamen.add(text_player_2.getText());
                }
              
                if (text_player_3.getText()!="")
                {
                    testnamen.add(text_player_3.getText());
                }
              
                if (text_player_4.getText()!="")
                {
                    testnamen.add(text_player_4.getText());
                }
              


                c_main_stage.setScene(c_sec_scene);
              
              
                gControl.setPlayer(Integer.parseInt(playerCount), testnamen);

              
            }

        });
      
        c_button_back_main.setOnAction(new EventHandler<ActionEvent>() {
          
            @Override
            public void handle(ActionEvent AE_gamer_button) {
              
                c_main_stage.setScene(c_main_scene);
              
            }

        });

    }
  
}

Habe nun schon das I-Net durchforstet und folgendes gefunden, als Lösung.
Java:
Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // entsprechende UI Komponente updaten
    }
});

Habe dieses Konstrukt um angegebene Codezeile gelegt, ohne dass sich jedoch etwas geändert hätte. Verschiedene Code, aus mehreren Quellen genommen und versucht. Es hat jedoch nichts geklappt.
Habt ihr vielleicht eine Idee? Kann es möglicherweise gar nicht an der Stelle liegen?
 

javampir

Bekanntes Mitglied
hi,
eine nullpointerexception heißt nun mal nichts anderes dass auf ein objekt zugegriffen wird, das null ist. es sieht so aus, wie wenn h_imgView_main null wäre. überprüfe das doch mal mit einer einfachen if davor
javampir
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
_user_q Objekte vom MainController in anderen Controller verwenden und andersherum (NullPointerException) AWT, Swing, JavaFX & SWT 14
Jose05 Nullpointerexception bei toArray() AWT, Swing, JavaFX & SWT 9
Jose05 NullPointerException bei Beschreiben eines Labels aus einem Array AWT, Swing, JavaFX & SWT 12
J Java FX NullPointerException, ObservableList wird in View nicht angezeigt.. AWT, Swing, JavaFX & SWT 34
N Nur bei einem Textfiled Nullpointerexception bei getText() AWT, Swing, JavaFX & SWT 1
R NullPointerException beim Start des Fensters AWT, Swing, JavaFX & SWT 1
D JavaFX Nullpointerexception wegen TableView AWT, Swing, JavaFX & SWT 2
ZH1896ZH JavaFX kann NullPointerException nicht auflösen... :( AWT, Swing, JavaFX & SWT 3
M NullPointerException / Controller AWT, Swing, JavaFX & SWT 4
R Swing Java9 NullPointerException bei JComboBox AWT, Swing, JavaFX & SWT 13
L java.lang.NullPointerException - Fehlermeldung AWT, Swing, JavaFX & SWT 2
M NullPointerException bei Übergabe eines RadioButtons AWT, Swing, JavaFX & SWT 11
zhermann NullPointerException bei Label.setText() AWT, Swing, JavaFX & SWT 5
it_is_all jLabel.setText -> Char in Str: NullpointerException AWT, Swing, JavaFX & SWT 1
D NullPointerException observableList AWT, Swing, JavaFX & SWT 4
J NullPointerException bei getText() AWT, Swing, JavaFX & SWT 3
K JFileChooser NullPointerException AWT, Swing, JavaFX & SWT 7
T Swing NullPointerException beim auslesen von jTextField - Anfänger! AWT, Swing, JavaFX & SWT 3
S Swing [CLOSED] NullPointerException bei BorderLayout() AWT, Swing, JavaFX & SWT 8
G Swing NullPointerException und JXImageView AWT, Swing, JavaFX & SWT 4
L exception in thread awt-eventqueue-0 java.lang.nullpointerexception AWT, Swing, JavaFX & SWT 2
U Swing NullPointerException trotz Referenz auf Textfield & Button AWT, Swing, JavaFX & SWT 6
L NullpointerException und Probleme mit repaint() AWT, Swing, JavaFX & SWT 11
G JavaFX NullPointerException bei Zugriff auf FXML Element AWT, Swing, JavaFX & SWT 0
K JavaFX Pfad öffen NullPointerException AWT, Swing, JavaFX & SWT 4
H SWT NullPointerException bei Zugriff auf Variablen des Hauptfensters AWT, Swing, JavaFX & SWT 6
L Event Handling windowAdapter - NullPointerException beim Schließen AWT, Swing, JavaFX & SWT 5
J JTable verursacht NullPointerException.Ratlos. AWT, Swing, JavaFX & SWT 5
C JButton NullPointerException AWT, Swing, JavaFX & SWT 2
Q getGraphics() - NullPointerException AWT, Swing, JavaFX & SWT 8
C NullPointerException im MVC Muster AWT, Swing, JavaFX & SWT 9
V Nullpointerexception (etwas mit thread und jframe) AWT, Swing, JavaFX & SWT 3
M JTable Drucken NullpointerException +JTable speichern AWT, Swing, JavaFX & SWT 6
T Swing Laden eines Icons auf JButton liefert NullPointerException AWT, Swing, JavaFX & SWT 8
M Exception in thread "Thread-3" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 18
C Swing NullPointerException beim Hinzufügen von Checkboxes in eine ArrayList AWT, Swing, JavaFX & SWT 5
1 JComponent -> java.lang.NullPointerException AWT, Swing, JavaFX & SWT 13
B Swing NullPointerException bei actionPerformed() AWT, Swing, JavaFX & SWT 2
E java.lang.NullPointerException AWT, Swing, JavaFX & SWT 14
R NullPointerException AWT, Swing, JavaFX & SWT 16
xehpuk Swing NullPointerException: Random-Instanz anfangs nicht initialisiert AWT, Swing, JavaFX & SWT 4
3 Swing NullPointerException in AW Event-0? AWT, Swing, JavaFX & SWT 5
C SWT NullPointerException AWT, Swing, JavaFX & SWT 12
J NullPointerException bei Benutzung vom TableCellRenderer AWT, Swing, JavaFX & SWT 3
A AWT Methodenaufruf "AWT-EventQueue-0" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 4
X NullPointerException beim Einfügen einer JMenuBar AWT, Swing, JavaFX & SWT 4
B NullPointerException bei JFrame AWT, Swing, JavaFX & SWT 2
D JPanel / NullPointerException AWT, Swing, JavaFX & SWT 4
G LookAndFeel Dynamisches Einlesen von LookAndFeels - NullPointerException AWT, Swing, JavaFX & SWT 4
E Login wird durch NullPointerException abgebrochen AWT, Swing, JavaFX & SWT 10
K NullPointerException ohne CodeLink AWT, Swing, JavaFX & SWT 6
L NullPointerException JFreeChart AWT, Swing, JavaFX & SWT 10
F NullPointerException - HILFE!! AWT, Swing, JavaFX & SWT 20
martinyyyy AWT Parameter erzeugen NullpointerException obwohl sie vorhanden sind? AWT, Swing, JavaFX & SWT 4
H Swing NullPointerException in JTable bei vielen Einträgen AWT, Swing, JavaFX & SWT 2
S Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 7
S NullPointerException bei JFileChooser AWT, Swing, JavaFX & SWT 8
X JList Probleme mit NullPointerException AWT, Swing, JavaFX & SWT 3
S NullPointerException bei Aufruf von updateUI() beim JFrame AWT, Swing, JavaFX & SWT 12
Z NullPointerException bei SplashScreen AWT, Swing, JavaFX & SWT 5
C NullPointerException bei Doppelpufferung AWT, Swing, JavaFX & SWT 3
F CardLayout.show() -> NullPointerException AWT, Swing, JavaFX & SWT 4
B NullPointerException bei String vergleich AWT, Swing, JavaFX & SWT 5
K NullPointerException bei JTextField AWT, Swing, JavaFX & SWT 8
L Problem mit "NullPointerException" bei grafischer AWT, Swing, JavaFX & SWT 3
D bei Thread in JComponent malen NullPointerException AWT, Swing, JavaFX & SWT 3
loadbrain JTextFields & java.lang.NullPointerException AWT, Swing, JavaFX & SWT 2
F JProgressBar und NullPointerException AWT, Swing, JavaFX & SWT 4
D javax.swing.Timer bringt NullPointerException AWT, Swing, JavaFX & SWT 4
R TextField bringt NullPointerException bei setVisible() AWT, Swing, JavaFX & SWT 12
A NullPointerException in einer JList AWT, Swing, JavaFX & SWT 7
F Fehler: java.lang.NullPointerException AWT, Swing, JavaFX & SWT 2
S Brauche Hilfe mit JFrame - NullPointerException AWT, Swing, JavaFX & SWT 7
A NullPointerException - warum? AWT, Swing, JavaFX & SWT 5
G NullPointerException bei Aufruf von JTabbedPane.getSelectedI AWT, Swing, JavaFX & SWT 7
G Problem mit NullPointerException AWT, Swing, JavaFX & SWT 11
M JTable verursacht NullPointerException AWT, Swing, JavaFX & SWT 8
M NullPointerException AWT, Swing, JavaFX & SWT 3
J NullPointerException bei getSelectedItem AWT, Swing, JavaFX & SWT 10
G Bekomme immer: NullPointerException AWT, Swing, JavaFX & SWT 2
M Javafx versuch Bibliothek zu erstellen AWT, Swing, JavaFX & SWT 0
E LayoutManager Erster Versuch mit Cardlayout AWT, Swing, JavaFX & SWT 2
S Versuch den Focus zu klauen detektieren? AWT, Swing, JavaFX & SWT 14
D Versuch JFileChooser in Gang zu setzen AWT, Swing, JavaFX & SWT 2
S JavaFX Fehler bei ImageView, Invalid URL AWT, Swing, JavaFX & SWT 6
Hatsi09 Javafx Neuladen von ImageView und MediaView AWT, Swing, JavaFX & SWT 3
H JavaFX Timeline II: Verschieben einer ImageView AWT, Swing, JavaFX & SWT 16
H JavaFX Timeline Bewegungen am ImageView nachvollziehen AWT, Swing, JavaFX & SWT 0
missy72 JavaFX Performance / ImageView in TableView über TimeLine AWT, Swing, JavaFX & SWT 1
D JavaFX Größe des Bild an ImageView anpassen AWT, Swing, JavaFX & SWT 3
Cromewell JavaFX ImageView vergößern wenn hovered AWT, Swing, JavaFX & SWT 2
S ImageView wird dem Gridpane nicht zugefügt AWT, Swing, JavaFX & SWT 1
R JavaFX Label oder Imageview anzeigen mit Buttonklick AWT, Swing, JavaFX & SWT 4
S Stretch Image in ImageView AWT, Swing, JavaFX & SWT 4
M JavaFX Bild zur ImageView in einem Kreis anzeigen. AWT, Swing, JavaFX & SWT 4
I ImageView in GridPane: Bildgröße AWT, Swing, JavaFX & SWT 1
I Bild über FXML (ImageView, Image) anzeigen AWT, Swing, JavaFX & SWT 1
J JavaFX ImageView rotieren lassen AWT, Swing, JavaFX & SWT 1
L JavaFX Image im ImageView auf Scroll Listener reagieren AWT, Swing, JavaFX & SWT 5
B JavaFX ImageView neues Bild zuweisen AWT, Swing, JavaFX & SWT 19

Ähnliche Java Themen


Oben