Problem Exception in thread "Thread-3" java.lang.N

0

Witam mam dziwny problem o to kod :

package mattoy;
import java.util.*;
/**
*

  • @Author Walperin
    */
    public class w1 extends Thread {
    Baza b;
    klasagraf z;

public void run()
{
for(int i=0; i<5; i++)
{
Random rand = new Random();
int liczba1 = rand.nextInt(14);
int liczba2 = rand.nextInt(5);
System.out.println(liczba1+" * "+liczba2);
z.wyswietl("Ile jest :"+liczba1+"*"+liczba2+" ?"); $$$**$$$ opis bledu ponizej

 b.set(liczba1,liczba2);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
 }

}
/** Creates a new instance of w1 */
public w1() {
}
}

$$**$$ Exception in thread "Thread-3" java.lang.NullPointerException
at mattoy.w2.run(w2.java:27) takie cos mi wyskakuje zawsze kiedy korzystam z metod klasy :

package mattoy;

/**
*

  • @Author Walperin
    /
    public class klasagraf extends javax.swing.JFrame {
    w1 watek1;
    w2 watek2;
    /
    * Creates new form klasagraf */
    public klasagraf() {
    initComponents();
    }

    /** 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.
      */
      // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
      private void initComponents() {
      jLabel1 = new javax.swing.JLabel();
      jButton1 = new javax.swing.JButton();
      jTextField1 = new javax.swing.JTextField();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      jLabel1.setBackground(new java.awt.Color(204, 204, 255));
      jLabel1.setText("text");
      jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

      jButton1.setText("jButton1");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      jButton1ActionPerformed(evt);
      }
      });

      jTextField1.setText("jTextField1");

      jLabel2.setText("0");

      jLabel3.setText("Punktacja");

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .addContainerGap()
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 6, Short.MAX_VALUE)
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jButton1)
      .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
      .add(51, 51, 51))
      .add(layout.createSequentialGroup()
      .add(jLabel3)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jLabel2)
      .addContainerGap())))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .addContainerGap()
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
      .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jButton1)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 63, Short.MAX_VALUE)
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
      .add(jLabel3)
      .add(jLabel2))
      .add(38, 38, 38))
      );
      pack();
      }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    // przycisk klik
    }

    /**

    • @param args the command line arguments
      */
      public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() { ******************* zawartosc napisana przeze mnie, od tad w dol

           w1 watek1= new w1();
           w2 watek2= new w2();
           klasagraf k = new klasagraf();
           Baza bb= new Baza();
           watek1.b=bb;
           watek2.b=bb;
           watek1.z=k;           
      
           new klasagraf().setVisible(true);
           watek1.start();
           watek2.start();
       }
      

      });
      }
      public void wyswietl(String zapytanie)
      {
      jLabel1.setText(zapytanie);
      }
      public String getTextField()
      {
      return jTextField1.getText();
      }
      public void ustawPkt(String punkt)
      {
      jLabel2.setText(punkt);
      } **************************** do tad
      // Variables declaration - do not modify
      private javax.swing.JButton jButton1;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JLabel jLabel2;
      private javax.swing.JLabel jLabel3;
      private javax.swing.JTextField jTextField1;
      // End of variables declaration

}
Dziekuje z gory za helpa

0

Kod wygląda ok. Parametry są przekazywane do wątku. Jak na razie jedyna uwaga dotyczy wyświetlenia nowej instancji klasy klasagraf. Przekazujesz obiekt k do wątku a wyświetlasz nowy obiekt klasy klasagraf. Zamiast new klasagraf().setVisible(true); daj k.setVisible(true); Będziesz wyświetlał to, na czym operuje wątek.

0

Dzieki, przynajmniej teraz wyswietla program zapytanie, ale w dalszym ciagu wyskakuje przy kompilacji : Exception in thread "Thread-3" java.lang.NullPointerException
at mattoy.w2.run(w2.java:27)
i pokazuje mi ze to dotyczy tej linijki : z.wyswietl("Ile jest :"+liczba1+"*"+liczba2+" ?");

0

Zdaje się, że pomieszałeś klasy. Po wyrzuceniu obiektów w2 i bb kod odpala się bez problemów. Błąd wywołuje wątek klasy w2 - tak głośi treść wyrzuconego wyjątku. Aby coś zaradzić potrzebny jest kod klasy w2.

1 użytkowników online, w tym zalogowanych: 0, gości: 1