0

Unable to setText for JLabel from another class.

hello, I am trying to change a JLabels text when a method it executed here's the code I have declaring the JLabel in the main class - JLabel textlab = new JLabel(); textlab.setBounds(33, 145, 189, 141); frmLauncher.getContentPane().add(textlab); } public void setLabelText (String text){ //textlab = new JLabel(); textlab.setText(text); } // Variables declaration - do not modify public javax.swing.JLabel textlab; // End of variables declaration and here is the code in the updater class that I want to run that will change the text inside the JLabel - static loop p = new loop(); <------------- here static String version = "0.0"; static boolean needDownload = false; public static void start(){ Main(); } public static void needDownload(){ { File f = new File(System.getProperty("user.home") + "/ver.txt"); try { List<String> lines = Files.readAllLines(Paths.get(f.getAbsolutePath()), StandardCharsets.UTF_8); for (String str : lines){ version = str; System.out.println("Version: " + str); p.setLabelText("Version: " + str); <------------ here } } catch (IOException e) { e.printStackTrace(); } } when I run the code I get "java.lang.NullPointerException" I understand what this error is as it's very common however I am unable to figure out what the cause of it this time is? any ideas thanks :3

16th Aug 2019, 8:28 PM
Premises
Premises - avatar
1 Odpowiedź
+ 2
Supposing than loop is the class containing a reference to target label, are you sure than when you create it you assign it to class field textlab? It seem than you have 2 textlab label which only local one is assigned...
17th Aug 2019, 6:13 AM
KrOW
KrOW - avatar