• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Debugging

I cant get this piece of code to work and I don't know what to try next, I'm thinking I have tried everything... So please have a look and try to help me fix the error which occurs:

The error is the common NullPointerException in Line 25 in the Data class. (See below)

/*
 * Controller.java
 *
 */
package pclab15;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Controller extends JFrame {
      

      Data data;

      public static JTextField tf_ja, tf_nei;
      private JPanel topp, v1, v2, bunn;
      private JChart kakediagram;
      private JLabel l_ja, l_nei;
      private JButton b_ja, b_nei;
      private String v_ja = "Ja", v_nei = "Nei";
      
      public Controller(Data d) {
            super("Model-View-Controller Demo");
            Lytter lytter = new Lytter();
            data = d;
          topp = new JPanel();
          v1 = new JChart();
          v2 = new JPanel();          
          bunn = new JPanel();
          b_ja = new JButton(v_ja);
          b_nei = new JButton(v_nei);
          l_ja = new JLabel("JA-stemmer");          
          l_nei = new JLabel("NEI-stemmer");
            tf_ja = new JTextField(5);
            tf_nei = new JTextField(5);
            tf_ja.setEnabled(false);
            tf_nei.setEnabled(false);
            tf_ja.setText("0");
            tf_nei.setText("0");
            l_ja.setEnabled(false);
            l_nei.setEnabled(false);
            
            topp.setBorder(BorderFactory.createTitledBorder("Kontroller"));
            v1.setBorder(BorderFactory.createTitledBorder("View #1"));
            v2.setBorder(BorderFactory.createTitledBorder("View #2"));
            
            b_ja.addActionListener(lytter);
            b_nei.addActionListener(lytter);
            
            v1.setLayout(new GridLayout(1,1,5,5));
            
            v2.setLayout(new GridLayout(2,2,5,5));
            v2.add(l_ja);
            v2.add(tf_ja);
            v2.add(l_nei);
            v2.add(tf_nei);
            
            topp.setLayout(new GridLayout(1,2,5,5));
            topp.add(b_ja);
            topp.add(b_nei);
            
            bunn.setLayout(new GridLayout(1,2,5,5));
            bunn.add(v1);
            bunn.add(v2);
            
          Container GUI = getContentPane();
            GUI.setLayout(new BorderLayout());
            
            GUI.add(topp, BorderLayout.NORTH);
            GUI.add(bunn, BorderLayout.CENTER);
            
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
            setSize(400,250);
      } // end constructor

      private class Lytter implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                  String cmd = e.getActionCommand();
                  if (cmd.equals(v_ja)) {
                        data.endreData(1);
                  } else if (cmd.equals(v_nei)) {
                        data.endreData(2);
                  } // end if
            } // end method actionPerformed
      } // end class Lytter
} // end class Controller

/*
 * Data.java
 *
 */
package pclab15;

import java.util.Observable;

public class Data extends Observable {
      private int data;
      private Visning visning;

      public Data(int verdi) {
            data = verdi;
      } // end contructor

      public void endreData(int i) {
            int CAntJa = Integer.parseInt(Controller.tf_ja.getText());
            int CAntNei = Integer.parseInt(Controller.tf_nei.getText());            
            int NAntJa = CAntJa + 1;
            int NAntNei = CAntNei + 1;
            if (i == 1) {
                  data = NAntJa;
            } else if (i == 2) {
                  data = NAntNei;
            } // end if
            if (i == 1) {
                  Object obj = "" + CAntJa;
                  if (data != CAntJa) {
                        visning.plusJa(obj); //<------- ERROR
                        setChanged();
                  } // end if
                  notifyObservers();
            } else if (i == 2) {
                  Object obj = "" + CAntNei;
                  if (data != CAntNei) {
                        visning.plusNei(obj);
                        setChanged();
                  } // end if
                  notifyObservers();
            } // end if
      } // end class endreData
   
      public String toString() {
            return "" + data;
      } // end class toString
} // end method Data

/*
 * Visning.java
 *
 */
package pclab15;

import org.jfree.chart.JFreeChart;

public class Visning {

      public Visning() {
      } // end contructor
      
      public void update(int i, int CAnt) {
            if (i != 0) {
                  Object obj = "" + CAnt;
                  if (i == 1) {
                        plusJa(obj);
                  } else if (i == 2) {
                        plusNei(obj);
                  } // end if                  
            } // end if
      } // end method update
      
      public void plusJa(Object obj) {
            int i = Integer.parseInt(obj.toString());
//            View #1
            JFreeChart chart = JChart.createChart(JChart.updateDataset(i));
//            View #2
            i += i;
            Controller.tf_ja.setText("" + i);
      } // end method plusJa
      
      public void plusNei(Object obj) {
            int i = Integer.parseInt(obj.toString());
//            View #1
            JFreeChart chart = JChart.createChart(JChart.updateDataset(i));
//            View #2
            i += i;
            Controller.tf_nei.setText("" + i);
      } // end method plusNei
} // end class Visning

/*
 * Klient.java
 *
 */
package pclab15;

public class Klient {
      public static void main (String args[]) {
            Data data = new Data(0);
            Visning visning = new Visning();
            Controller kontroller = new Controller(data);
      } // end method main
} // end class Klient

/*
 * JChart.java
 *
 */
package pclab15;

import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * A simple demonstration application showing how to create a pie chart using
 * data from a {@link DefaultPieDataset}.
 */
public class JChart extends JPanel {
      private static DefaultPieDataset dataset = new DefaultPieDataset();
   
    /**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public JChart() {
            JFreeChart chart = createChart(createDataset());
    }

    /**
     * Creates a simple dataset.
     *
     * @return a dataset.
     */
    public static PieDataset createDataset() {
        dataset.setValue("Ja", new Double(0));
        dataset.setValue("Nei", new Double(0));
        return dataset;  
    }
      
      /**
       * Updates current dataset
       *
       * @param i            chosen value "Ja"(1) or "Nei"(2)
       *
       * @return an updated dataset.
       */
    public static PieDataset updateDataset(int i) {
          if (i == 1) {
                int j = dataset.getValue("Ja").intValue();
                j += j;
                dataset.setValue("Ja", j);
          } else if (i == 2) {
                  int n = dataset.getValue("Nei").intValue();
                  n += n;
                  dataset.setValue("Nei", n);
          }
          return dataset;
    }
   
    /**
     * Creates a chart.
     *
     * @param dataset  the dataset.
     *
     * @return a chart.
     */
    public static JFreeChart createChart(PieDataset dataset) {
       
        JFreeChart chart = ChartFactory.createPieChart(
            "",  // chart title
            dataset,             // data
            true,               // include legend
            true,
            false
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("Finner ikke noe data");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;
       
    }
}

(Uses JFreeChart libraries)

Any help appreciated,
eX.



PS. I won't be able to respond untill I get back on the 18th.
0
Gaute Rønningen
Asked:
Gaute Rønningen
  • 4
  • 3
  • 2
2 Solutions
 
aozarovCommented:
In your Data class you never created an instance of Visning and therefore visning variable is null
0
 
aozarovCommented:
Probably you want to change:
public Data(int verdi) {
          data = verdi;
     } // e

to
public Data(int verdi) {
          data = verdi;
visning = new ();
     } // e
0
 
cjjcliffordCommented:
you've don't assign anything to the "visning" attribute, try changing the constructor to

ublic Data(int verdi) {
          data = verdi;
          visning = new Visning();
     } // end contructor
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aozarovCommented:
Oops, just realized that I didn't paste the Visning part.
visning = new (); -> visning = new Visning (); (as cjjclifford pointed out).

0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
Already have the:
Visning visning = new Visning();
included in the Klient.java, but even though it was already there I had to try pasting it in, unfortunately there was no change...
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
Well, there was no error, but the program still doesn't work properly...
0
 
cjjcliffordCommented:
what's the error now?

What's the program supposed to do?
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
The program is supposed to be a kind of voting application. You press the "Ja" button (=Yes) or the "Nei" button (=No) and you get a pie chart, which by the way doesn't work properly either (but I'll create a new question for that later), with the votes and a history on the side. So say you press 3 times on Yes and 2 times on No you'll get a pie chart which got 60% Yes and 40% No and some counters which says how many of each.

Now I added these lines in the endreData() method (Data.java)
            String temp = "D: " + data + " - V: " + i;
            if (i == 1) {
                  temp = temp + " - CurrentJa: " + CAntJa;
                  visning.update(i, CAntJa);
                  setChanged();
                  notifyObservers();
            } else if (i == 2) {
                  temp = temp + " - CurrentNei: " + CAntNei;
                  visning.update(i, CAntNei);
                  setChanged();
                  notifyObservers();
            } // end if
            System.out.println(temp);
Tried removing some useless lines too. Which prints the following:
D: 0 - V: 1 - CurrentJa: 0
D: 0 - V: 2 - CurrentNei: 0
D: 0 - V: 2 - CurrentNei: 0
D: 0 - V: 1 - CurrentJa: 0
D: 0 - V: 1 - CurrentJa: 0
When I skip the usage of the update method (Visning.java) going straight for the plusJa and plusNei methods, I get the following lines:
D: 0 - V: 1 - CurrentJa: 0
D: 0 - V: 2 - CurrentNei: 0
D: 0 - V: 2 - CurrentNei: 2
D: 0 - V: 1 - CurrentJa: 4
D: 0 - V: 1 - CurrentJa: 4
Which means it goes to 2 on Yes and 4 on No and stops...
0
 
Gaute RønningenOwner, DeveloperAuthor Commented:
Creating new question...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now