Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Junky

Thread: double cannot be dereferenced

  1. #1
    Garcia65 is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default double cannot be dereferenced

    Hi I am new to Java. I keep getting this error that says double cannot be dereferenced, below is my code. Please don't yell at me to much as I have only been writing Java for about 3 weeks! Thanks for any help you can provide.

    import java.text.DecimalFormat;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author Andy
    */
    public class javagui extends javax.swing.JPanel {

    //2d array
    double[][] timeWage;
    //position in array
    int timeWageIndex = 0;
    //minimum wage
    double minimumWage = 6.55;
    static final String lineSeparator = System.getProperty("line.separator");
    private JLabel tutMinsLabel;
    private JLabel earningsLabel;
    private JScrollPane jScrollPane1;
    private JTextArea reportTextArea;
    private JTextField tutorTimeText;
    private JTextField paymentText;
    private JButton enterButton;
    private JButton reportButton;
    private JButton quitButton;
    private double tutorTime;
    private double payment;
    private JLabel tutorTimeLabel;




    /** Creates new form GUI */
    public javagui() {
    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.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


    tutorTimeLabel = new javax.swing.JLabel();
    earningsLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    reportTextArea = new javax.swing.JTextArea();
    tutorTimeText = new javax.swing.JTextField();
    paymentText = new javax.swing.JTextField();
    enterButton = new javax.swing.JButton();
    reportButton = new javax.swing.JButton();
    quitButton = new javax.swing.JButton();

    tutorTimeLabel.setText("Enter total tutoring time in minutes:");

    earningsLabel.setText("Enter total amount earned this session:");

    reportTextArea.setColumns(20);
    reportTextArea.setRows(5);
    jScrollPane1.setViewportView(reportTextArea);

    enterButton.setText("Enter");
    enterButton.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    enterButtonActionPerformed(evt);
    }
    });

    reportButton.setText("Report");
    reportButton.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    reportButtonActionPerformed(evt);
    }
    });

    quitButton.setText("Quit");
    quitButton.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    quitButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(23, 23, 23)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
    .addComponent(enterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(reportButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING, false)
    .addComponent(tutMinsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(earningsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
    .addComponent(paymentText)
    .addComponent(tutorTimeText, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))))
    .addContainerGap(58, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(tutorTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(24, 24, 24)
    .addComponent(tutMinsLabel)))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(earningsLabel)
    .addComponent(paymentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(enterButton)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(reportButton)
    .addGap(18, 18, 18)
    .addComponent(quitButton)
    .addContainerGap(320, Short.MAX_VALUE))
    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)))
    );
    }// </editor-fold>

    private void enterButtonActionPerformed(java.awt.event.ActionEv ent evt) {
    // TODO add your handling code here:


    //Code Exception error messages when data keyed incorrectly for both textfields
    double time = 0;
    double wage = 0;

    //Validates time
    try {
    time = Double.parseDouble(tutorTime.getText());
    } catch (NumberFormatException e1) {
    JOptionPane.showMessageDialog(null, "The time must be a decimal number", "Invalid Input",JOptionPane.ERROR_MESSAGE);
    }

    //Validate wage
    try {
    wage = Double.parseDouble(payment.getText());
    } catch (NumberFormatException e1) {
    JOptionPane.showMessageDialog(null, "The wage must be a decimal number", "Invalid Input",JOptionPane.ERROR_MESSAGE);
    }

    //Time greater than 0 and less than 4 hours
    if(time<=0 || time>240){
    JOptionPane.showMessageDialog(null, "The time must greater than 0 and less than or equal to 4 hours (240 min)",
    "Invalid Input",JOptionPane.ERROR_MESSAGE);
    }
    else{
    //Wage greater than 0
    if(wage<=0){
    JOptionPane.showMessageDialog(null, "The wage must greater than 0",
    "Invalid Input",JOptionPane.ERROR_MESSAGE);
    }
    else{
    //update array values
    timeWage[timeWageIndex][0] = time;
    timeWage[timeWageIndex][1] = wage;

    //increment index
    timeWageIndex++;

    //Reset text for user entry
    tutorTime.setText("");
    payment.setText("");
    }


    private void quitButtonActionPerformed(java.awt.event.ActionEve nt evt) {
    //select to close the Calculator
    System.exit(0);

    }

    private void reportButtonActionPerformed(java.awt.event.ActionE vent evt) {
    // TODO add your handling code here:

    reportTextArea.setText(""); // clear previous report
    String newline = "\n";
    double timeTotal = 0;
    double wageTotal = 0;


    //Loops through each entry
    for(int i = 0; i<=timeWageIndex-1; i++){
    //Gets current entry time
    double time = timeWage[i][0];
    //Gets current entry wage
    double wage = timeWage[i][1];
    //Adds to sums
    timeTotal += time;
    wageTotal += wage;

    reportTextArea.append("Minutes = " + time + " Earnings = $" + wage + newline);
    }

    DecimalFormat df = new DecimalFormat("#.##");
    double average = wageTotal / (timeTotal/60);
    //Displays report
    reportTextArea.append(newline + newline + "*****************************" + newline + newline + newline);
    reportTextArea.append("Report of your wages to Date:" + newline + newline);
    reportTextArea.append("Total Minutes Spent Tutoring = " + timeTotal + newline);
    reportTextArea.append("Total Earnings = $" + wageTotal + newline);
    reportTextArea.append("Average Per Hour Wage = $" + df.format(average) + newline + newline);
    reportTextArea.append("Minimum Wage is currently: $" + minimumWage + newline);

    if(average<minimumWage) {
    reportTextArea.append("Your average wages are less than average");
    }
    else if(average>minimumWage && average < minimumWage*2.0) {
    reportTextArea.append("Your average wages are average");
    }
    else if(average >= minimumWage*2.0) {
    reportTextArea.append("Your average wages are above average");
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: double cannot be dereferenced

    When dealing with objects you access them through a reference. When you call a method it is called dereferencing. Since double is a primitive and primitives do not have methods this is why you are getting this error.
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: double cannot be dereferenced

    You don't need to post pages of code when only a few lines will do. In particular you should post the lines causing the error.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: double cannot be dereferenced

    Also please use [code] tags [/code] when posting code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. ERROR - char cannot be dereferenced HELP
    By jonytek in forum New To Java
    Replies: 6
    Last Post: 05-04-2011, 06:54 AM
  2. int cannot be dereferenced
    By jackrush in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2010, 08:47 AM
  3. How can I solve "void cannot be dereferenced" error?
    By fervent07 in forum New To Java
    Replies: 2
    Last Post: 04-05-2010, 08:54 PM
  4. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 01:49 AM
  5. Replies: 2
    Last Post: 11-30-2008, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •