4 Replies - 342 Views - Last Post: 20 December 2013 - 03:21 PM Rate Topic: -----

#1 caoagas.michael1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-November 13

int cannot be dereferenced

Posted 20 December 2013 - 04:41 AM

hi guys im new to java and im receiving an error "int cannot be dereferenced" any help is well appreciated
import java.awt.*;
import java.awt.event.*;
public class finals extends Frame implements ItemListener{

Label lblstore = new Label("Torres Bulalohan and Ice Cream");
Label lblmenu = new Label("Menu Course");
Label lblquantity = new Label("Quantity");
Label lbltotal = new Label("Total");
Checkbox chkbox1 = new Checkbox("Bulalo");
TextField txtq1 = new TextField();
Button btnsub1 = new Button("Sub");
Label lblline1 = new Label("__________");
Checkbox chkbox2 = new Checkbox("Sisig");
TextField txtq2 = new TextField();
Button btnsub2 = new Button("Sub");
Label lblline2 = new Label("__________");
Checkbox chkbox3 = new Checkbox("Steak");
TextField txtq3 = new TextField();
Button btnsub3 = new Button("Sub");
Label lblline3 = new Label("__________");
Label lblic = new Label("ICE CREAM");
CheckboxGroup chflavor = new CheckboxGroup();
Checkbox chvanilla = new Checkbox("Vanilla", chflavor, true);
Checkbox chhalo = new Checkbox("Halo 2x", chflavor, true);
Label lblcone = new Label("Select Cone");
Choice chcone = new Choice();
Button btntotal1 = new Button("Total");
Label lblline4 = new Label("__________");
Label lblsc = new Label("Senior Citizen");
CheckboxGroup chsc = new CheckboxGroup();
Checkbox chyes = new Checkbox("Yes", chflavor, true);
Checkbox chno = new Checkbox("No", chflavor, true);
Button btntotal2 = new Button("Total");
Label lbldiscount = new Label("Discount:");
Label lblline5 = new Label("__________");
Label lblgt = new Label("Grand Total:");
Label lblline6 = new Label("__________");

int num1, num2, num3, ans1, ans2, ans3, ans4, ans5, ans6, ans7, answer1;
    
    public finals() {
        
    setTitle("Finals");
    setLocation(50,30);
    setSize(400,500);
    setResizable(true);
    setLayout(null);
    
    

    lblstore.setBounds(100,80,300,25);
    add(lblstore);
    

    lblmenu.setBounds(150,120,100,25);
    add(lblmenu);
    
    
    lblquantity.setBounds(100,140,50,25);
    add(lblquantity);
    
    
    lbltotal.setBounds(300,140,50,25);
    add(lbltotal);
    
    
    chkbox1.setBounds(20,170,50,25);
    chkbox1.addItemListener(this);
    add(chkbox1);
    
    
    txtq1.setBounds(100,170,50,20);
    add(txtq1);
        
    
    btnsub1.setBounds(175,170,50,20);
    add(btnsub1);
    
    lblline1.setBounds(280,170,70,15);
    add(lblline1);
    
    answer1.setBounds(280,170,70,15); //error section
    add(answer1);
    
    chkbox2.setBounds(20,190,50,25);
    chkbox2.addItemListener(this);
    add(chkbox2);
    
    
    txtq2.setBounds(100,190,50,20);
    add(txtq2);
    
    
    btnsub2.setBounds(175,190,50,20);
    add(btnsub2);
        
    
    lblline2.setBounds(280,190,70,15);
    add(lblline2);
    
    
    chkbox3.setBounds(20,210,50,25);
    chkbox3.addItemListener(this);
    add(chkbox3);
    
    
    txtq3.setBounds(100,210,50,20);
    add(txtq3);
    
    
    btnsub3.setBounds(175,210,50,20);
    add(btnsub3);
        
    
    lblline3.setBounds(280,210,70,15);
    add(lblline3);
    
    
    lblic.setBounds(150,250,100,25);
    add(lblic);
    
    
    
    chvanilla.setBounds(100,270,70,25);
    add(chvanilla);
    
    
    chhalo.setBounds(175,270,80,25);
    add(chhalo);
    
    
    lblcone.setBounds(90,300,80,25);
    add(lblcone);
    
    
    chcone.add("Waffer");
    chcone.add("Sugar");
    chcone.setBounds(175,300,80,20);
    add(chcone);
    
    
    btntotal1.setBounds(100,335,50,20);
    add(btntotal1);
        
    
    lblline4.setBounds(280,335,70,15);
    add(lblline4);
    
    
    lblsc.setBounds(80,370,80,20);
    add(lblsc);
    
    
    
    chyes.setBounds(80,390,50,25);
    add(chyes);
 
    
    chno.setBounds(130,390,50,25);
    add(chno);
    
    
    btntotal2.setBounds(210,370,50,20);
    add(btntotal2);
    
    
    lbldiscount.setBounds(210,395,60,25);
    add(lbldiscount);
        
    lblline5.setBounds(280,400,70,15);
    add(lblline5);
    
    
    lblgt.setBounds(195,415,80,25);
    add(lblgt);
        
    
    lblline6.setBounds(280,420,70,15);
    add(lblline6);
    
    setVisible(true);
}

public void  itemStateChanged(ItemEvent e){
    
    if(e.getSource() == chkbox1 && e.getSource() == btnsub1){
        
        if(e.getStateChange() == ItemEvent.SELECTED){
            ans1 = num1 * 30;
            
            answer1.setText(Integer.toString(ans1));
        }
        
    }
    
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: int cannot be dereferenced

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: int cannot be dereferenced

Posted 20 December 2013 - 05:03 AM

Where is the error line number?
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: int cannot be dereferenced

Posted 20 December 2013 - 05:14 AM

answer1 is an int, why? int doesn't have setBounds method.

This post has been edited by Michael26: 20 December 2013 - 05:16 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: int cannot be dereferenced

Posted 20 December 2013 - 07:20 AM

The code you're writing is about a decade out of date. That is, most modern Java GUI will use Swing or something based on it. Using absolute positioning has always been questionable.

Do you really, really need to hang on to all those variable references? It's very confusing. The labels, in particular, probably just need to be added. Consider using methods to make your life easier.

e.g.
private void addLabel(int x, int y, int width, int height, String text) {
	Label comp = new Label(text);
	comp.setBounds(x, y, width, height);
	add(comp);
}
//...
addLabel(100, 80, 300, 25, "Torres Bulalohan and Ice Cream");
addLabel(150, 120, 100, 25, "Menu Course");
addLabel(100, 140, 50, 25, "Quantity");
//...


Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: int cannot be dereferenced

Posted 20 December 2013 - 03:21 PM

Use of null layout is also poor practice. It makes it so the program doesn't resize well, which breaks an expectation of the user. Consider looking into a GridLayout instead. Note that GridLayout doesn't respect preferred sizes, but surrounding components with Boxes, then adding the Boxes to the GridLayout will resolve this. Your GUI will also scale better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1