...

View Full Version : Basic Array Help



hammer55
06-26-2006, 09:58 AM
Hi, I am pretty new at this stuff, so please ignore my lack of knowledge.
Anyway, I'm trying to teach myself arrays, and I could use some help. I'm not familiar to posting these kind of help requests, so i hope my format is ok.

I have an already established class called book.java which has variables and mutators/accessors for author, title, cost, total (in stock) remain (remain in stock), year, month and date.

I have a GUI working for it. Now I am trying to use "Add" to add the data to an object in an array, and List to list ALL the books in an array. My problem seems to be in the Add button function. It doesn't add the data to an array, nor does it get to clearing the fields.

Here is the full code for it. (guibook.java). I can post my book class if you'd like.


import javax.swing.*;
import BreezySwing.*;

public class guibook extends GBFrame {

private JLabel titlelabel;
private JLabel authorlabel;
private JLabel costlabel;
private JLabel totallabel;
private JLabel remainlabel;
private JLabel yearlabel;
private JLabel monthlabel;
private JLabel datelabel;
private JTextField titlefield;
private JTextField authorfield;
private DoubleField costfield;
private IntegerField totalfield;
private IntegerField remainfield;
private IntegerField yearfield;
private IntegerField monthfield;
private IntegerField datefield;
private JButton add;
private JButton list;
private JTextArea output;
private book[] b = new book[20];
private int i = 0;

public guibook() {

titlelabel = addLabel ("Title" ,1,1,1,1);
authorlabel = addLabel ("Author" ,1,2,1,1);
costlabel = addLabel ("Cost" ,1,3,1,1);
totallabel = addLabel ("Total In Stock" ,1,4,1,1);
remainlabel = addLabel ("Remaining in Stock" ,1,5,1,1);
yearlabel = addLabel ("Year" ,3,2,1,1);
monthlabel = addLabel ("Month" ,3,3,1,1);
datelabel = addLabel ("Date" ,3,4,1,1);

titlefield = addTextField ("" ,2,1,1,1);
authorfield = addTextField ("" ,2,2,1,1);
costfield = addDoubleField (0.0 ,2,3,1,1);
totalfield = addIntegerField (0 ,2,4,1,1);
remainfield = addIntegerField (0 ,2,5,1,1);
yearfield = addIntegerField (0 ,4,2,1,1);
monthfield = addIntegerField (0 ,4,3,1,1);
datefield = addIntegerField (0 ,4,4,1,1);

add = addButton ("Add" ,5,2,1,1);
list = addButton ("List" ,5,4,1,1);
output = addTextArea ("Title Author Cost Total Amount In Stock Year Month Date \n"
,6,1,5,1);
}

public void buttonClicked (JButton buttonObj){

if (buttonObj == add){

b[i].setTitle(titlefield.getText());
b[i].setAuthor(authorfield.getText());
b[i].setCost(costfield.getNumber());
b[i].setTotal(totalfield.getNumber());
b[i].setRemain(remainfield.getNumber());
b[i].setYear(yearfield.getNumber());
b[i].setMonth(monthfield.getNumber());
b[i].setDate(datefield.getNumber());

titlefield.setText("");
authorfield.setText("");
costfield.setNumber(0);
totalfield.setNumber(0);
remainfield.setNumber(0);
yearfield.setNumber(0);
monthfield.setNumber(0);
datefield.setNumber(0);
i++;
}
else {

for(int c = 0; c<=i; c++) {
output.setText("Title Author Cost Total Amount In Stock Year Month Date \n");
output.append(Format.justify('l', b[c].getTitle(), 20));
output.append(Format.justify('l', b[c].getAuthor(), 15));
output.append(Format.justify('r', b[c].getCost(),6,2));
output.append(Format.justify('r', b[c].getTotal(),8));
output.append(Format.justify('r', b[c].getRemain(),17));
output.append(Format.justify('r', b[c].getYear(),13));
output.append(Format.justify('r', b[c].getMonth(),9));
output.append(Format.justify('r', b[c].getDate(),11));
}
}
}


public static void main (String[] args){
guibook theGUI = new guibook();
theGUI.setSize (500, 100); //Set the window's size in pixels
// width = 250, height = 100
theGUI.setVisible (true); //Make the window visible
}
}

vwphillips
06-26-2006, 10:00 AM
you have posted JAVA code

this is a JavaScript forum

Very similar but very different.

and please dont keep PMing unless you are a paying client.

you have done it again!

I have put you on my ignore list

_Aerospace_Eng_
06-26-2006, 10:05 AM
Yes that is correct you posted in the incorrect forum however we need to see your book class as well. Please use code tags to post code, its the # sign up top. Don't cross post this. A moderator will move it.

hammer55
06-26-2006, 10:10 AM
I will post my book class, but for the record, when I just instantiated a new book (b1), it would add and list ok.
EDIT: My apologies vwphillips.


public class book { // contstructor

private String title, author;
private double cost;
private int total, remain, year, month, date;

public book() {
title = "";
author = "";
cost = 0;
total = 0; // total books in inventory
remain = 0; // number of books left
year = 0;
month = 0;
date = 0;
}
public book (String tit, String au, double co, int tot, int re, int ye, int mo, int da) {
//overloaded constructor
title = tit;
author = au;
cost = co;
total = tot;
remain = re;
year = ye;
month = mo;
date = da;
}

//mutators
public void setTitle(String ti){
title = ti;
}
public void setAuthor(String a) {
author = a;
}
public void setCost(double c) {
cost = c;
}
public void setTotal (int to){
total = to;
}
public void setRemain (int r) {
remain = r;
}
public void setYear (int y) {
year = y;
}
public void setMonth (int m) {
month = m;
}
public void setDate (int d) {
date = d;
}

//accessors
public String getTitle(){
return title;
}
public String getAuthor() {
return author;
}
public double getCost() {
return cost;
}
public int getTotal() {
return total;
}
public int getRemain() {
return remain;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDate() {
return date;
}
//toString method
public String toString() {
return "The book is "+title+" by "+author+". \nThere are "+remain+" copies out of "+total+
" in stock. \nOriginal cost was "+cost+"\nThe year of the book is "+year+" in the month of "+month+
" on this date "+date;
}

//irrelevant code removed here (served totally different purpose)

}

_Aerospace_Eng_
06-26-2006, 10:13 AM
Okay I think the problem might be because of your variables being set to private. Try setting them to public for both your gui class and book class. You should probably be using this.var for example something like this

this.title = "";
this.author = "";
this.cost = 0;
this.total = 0; // total books in inventory
this.remain = 0; // number of books left
this.year = 0;
this.month = 0;
this.date = 0;

hammer55
06-26-2006, 10:18 AM
I don't think my teacher wanted us to do that. Is there any other way without changing all the variables...

Again, if I do not use arrays and if I declare private book b1 and then in my add function use b1 = new book() instead of the b[i] and b[c] everything works.

_Aerospace_Eng_
06-26-2006, 10:30 AM
What compiler are you using?

hammer55
06-26-2006, 10:31 AM
I am using Free Java 2.0. I'm not sure if it's still available.

_Aerospace_Eng_
06-26-2006, 11:12 AM
Hmm what JDK are you using? Where are you getting the breezy package from?

hammer55
06-26-2006, 11:15 AM
I am using JDK 1.5. The breezy package comes from an examples folder to be used with the course. It has not failed myself or anyone else yet.

EDIT: Compare the following add function before arrays were added

under declarations

private book b1;


under buttonClicked

public void buttonClicked (JButton buttonObj){

if (buttonObj == add){
b1 = new book(titlefield.getText(), authorfield.getText(), costfield.getNumber(), totalfield.getNumber(), remainfield.getNumber(), yearfield.getNumber(), monthfield.getNumber(), datefield.getNumber());
titlefield.setText("");
authorfield.setText("");
costfield.setNumber(0);
totalfield.setNumber(0);
remainfield.setNumber(0);
yearfield.setNumber(0);
monthfield.setNumber(0);
datefield.setNumber(0);
}
else {
output.setText("Title Author Cost Total Amount In Stock Year Month Date \n");
output.append(Format.justify('l', b1.getTitle(), 20));
output.append(Format.justify('l', b1.getAuthor(), 15));
output.append(Format.justify('r', b1.getCost(),6,2));
output.append(Format.justify('r', b1.getTotal(),8));
output.append(Format.justify('r', b1.getRemain(),17));
output.append(Format.justify('r', b1.getYear(),13));
output.append(Format.justify('r', b1.getMonth(),9));
output.append(Format.justify('r', b1.getDate(),11));

}


That worked 100%. Only thing is it overwrote b1 everytime. That's why now I have to make an array of books.

_Aerospace_Eng_
06-26-2006, 11:34 AM
TBH I've never even used that package. Since I'm not entirely sure how that package works. All I can give you is the errors that my compiler spit out from clicking on the add button. And yes I have the Breezy package.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at guibook.buttonClicked(guibook.java:58)
at BreezySwing.GBFrameButtonListener.actionPerformed(GBFrame.java:518)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum