2017-04-13 9 views
0

申し訳ありません。私はので、私は、Javaのためのsmartmirrorを作ってるんだ私にJavaFXのnullポインタ例外()

をご了承ください英語も話せませんが、私はのsetText()関数に問題があります。

天気APIを呼び出し、変数に場所名を保存した後、私は開始label.setText()が、それはnullポインタ例外があります。

は、私は後でplatform.runについて()メソッドとタスクを聞いたが、彼らは動作しません。

私のソースのすべての

package SmartMirror.main; 

import java.io.IOException; 
public class SpeechClass { 
WeatherController weather = new WeatherController(); 


// Logger 
private Logger logger = Logger.getLogger(getClass().getName()); 

// Variables 
public String result; 

// Threads 
Thread speechThread; 
Thread resourcesThread; 
Thread openThread; 

// LiveRecognizer 
private LiveSpeechRecognizer recognizer; 

protected String location; 

public void Speech(){ 
    // Loading Message 
    logger.log(Level.INFO, "Loading..\n"); 

    // Configuration 
    Configuration configuration = new Configuration(); 

    // Load model from the jar 
    configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); 
    configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict"); 

    // if you want to use LanguageModelPath disable the 3 lines after which 
    // are setting a custom grammar-> 

    // configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin") 

    // Grammar 
    configuration.setGrammarPath("resource:/grammars"); 
    configuration.setGrammarName("grammar"); 
    configuration.setUseGrammar(true); 

    try { 
     recognizer = new LiveSpeechRecognizer(configuration); 
    } catch (IOException ex) { 
     logger.log(Level.SEVERE, null, ex); 
    } 

    // Start recognition process pruning previously cached data. 
    recognizer.startRecognition(true); 

    // Start the Thread 
    startSpeechThread(); 
    startResourcesThread(); 

} 


/** 
* Starting the main Thread of speech recognition 
*/ 
protected void startSpeechThread() { 

    // alive? 
    if (speechThread != null && speechThread.isAlive()) 
     return; 

    // initialise 
    speechThread = new Thread(() -> { 
     logger.log(Level.INFO, "You can start to speak...\n"); 
     try { 
      while (true) { 
       /* 
       * This method will return when the end of speech is 
       * reached. Note that the end pointer will determine the end 
       * of speech. 
       */ 
       SpeechResult speechResult = recognizer.getResult(); 
       if (speechResult != null) { 

        result = speechResult.getHypothesis(); 

        System.out.println("You said: [" + result + "]\n"); 

        if(result.equals("one")){ 
         System.out.println("startOpenThread"); 
         startWeatherThread(); 
         openThread.sleep(3000); 

         Platform.runLater(new Runnable(){ 
          @Override 
          public void run(){ 
           weather.setLabel(); 
           openweather(); 
          } 
         }); 

        } 
        // logger.log(Level.INFO, "You said: " + result + "\n") 

       } else 
        logger.log(Level.INFO, "I can't understand what you said.\n"); 

      } 
     } catch (Exception ex) { 
      logger.log(Level.WARNING, null, ex); 
     } 

     logger.log(Level.INFO, "SpeechThread has exited..."); 
    }); 


    // Start 
    speechThread.start(); 

} 



/** 
* Starting a Thread that checks if the resources needed to the 
* SpeechRecognition library are available 
*/ 
protected void startResourcesThread() { 

    // alive? 
    if (resourcesThread != null && resourcesThread.isAlive()) 
     return; 

    resourcesThread = new Thread(() -> { 
     try { 

      // Detect if the microphone is available 
      while (true) { 
       if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) { 
        // logger.log(Level.INFO, "Microphone is available.\n") 
       } else { 
        // logger.log(Level.INFO, "Microphone is not 
        // available.\n") 

       } 

       // Sleep some period 
       Thread.sleep(350); 
      } 

     } catch (InterruptedException ex) { 
      logger.log(Level.WARNING, null, ex); 
      resourcesThread.interrupt(); 
     } 
    }); 

    // Start 
    resourcesThread.start(); 
} 

protected void startWeatherThread() { 
    try{ 
     openThread = new Thread(() -> { 
      weather.Weather(); // 날씨를 변수에 저장 

     }); 

    } catch (Exception e){ 

    } 
    // Start 
    openThread.start(); 
} 

public void openweather(){ 
    Stage dialog = new Stage(StageStyle.TRANSPARENT); 
    dialog.initModality(Modality.WINDOW_MODAL); 
    dialog.initOwner(null); 

    Parent parent = null; 
    try { 
     parent = FXMLLoader.load(WeatherController.class.getResource("weather_scene.fxml")); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    Scene scene = new Scene(parent); 
    dialog.setScene(scene); 
    dialog.setResizable(false); 
    dialog.show(); 
} 


} 


package SmartMirror.Weather; 

import java.io.BufferedReader; 


public class WeatherController { 

@FXML private Label labelLocation; 

public String locationResult="", weatherResult="", tempResult=""; 


    //날씨 API 
    public void Weather() { 
     try{ 

      //OpenAPI call하는 URL 
      String urlstr = "http://api.openweathermap.org/data/2.5/weather?" 
         +"q=Chuncheon" 
         +"&appid=f1bccf50c733316db790a00a2d5165c6&units=metric"; 
      URL url = new URL(urlstr); 
      BufferedReader bf; 
      String line; 
      String result=""; 

      //날씨 정보를 받아온다. 
      bf = new BufferedReader(new InputStreamReader(url.openStream())); 

      //버퍼에 있는 정보를 문자열로 변환. 
      while((line=bf.readLine())!=null){ 
       result=result.concat(line); 
       //System.out.println(line); 
      } 

      //문자열을 JSON으로 파싱 
      JSONParser jsonParser = new JSONParser(); 
      JSONObject jsonObj = (JSONObject) jsonParser.parse(result); 

      //날씨 출력 
      JSONArray weatherArray = (JSONArray) jsonObj.get("weather"); 
      JSONObject weather = (JSONObject) weatherArray.get(0); 

      //온도출력 
      JSONObject mainArray = (JSONObject) jsonObj.get("main"); 
      double ktemp = Double.parseDouble(mainArray.get("temp").toString()); 

      locationResult = (String) jsonObj.get("name"); 
      weatherResult = (String) weather.get("main"); 
      tempResult = Double.toString(ktemp) + "℃"; 


      System.out.println("startWeatherThread" + locationResult); 


      bf.close(); 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 


    } 


    public void setLabel(){ 
     Platform.runLater(new Runnable(){ 
      @Override 
      public void run(){ 
       System.out.println(locationResult); 
       labelLocation.setText(locationResult); 
      } 
     }); 

    } 

}

+0

にしてください。コントローラ上で 'setLabel()'を呼び出すのではなく、別のオブジェクト(コントローラと同じクラスになります) 。明らかに、FXMLファイルのロードの一部として作成された '@FXML'という注釈が付けられたフィールドは、実際のコントローラでのみ初期化されます。 –

答えて

0

まずがありますTT 私を助けてください、私はあなたの3国民が変化し、私はあなたがWhy use getters and setters?をチェックすることをお勧め、パブリック属性を使用して推薦していませんよフィールドをプライベートなものに変換し、getterとsetterを作成します。

あなたの問題について、私はあなたのfxmlコードを見ていませんでしたが、ラベルにはfx:idとも呼ばれるIDを付けるようにして、nullにならないようにしてください。 WeatherControllerの参照をfx:controller