0

Here's the whole code, i cant explain clearly what i want with my codes but maybe you can understand what i want:

import javax.swing.*;
public class Library 
{
    private Library[] items = new Library[5];
    private int counter = 0;
    private int xtr = 0;
    private int id=0;
    private String date_acquisition="";
    private String description="";
    private int num_copies=0;
    private String title="";
    public void setId(int temp_id)
    {
        id = temp_id;
    }
    public int getId()
    {
        return id;
    }

    public void setDateAcquisition(String temp_date_acquisition)
    {
        date_acquisition = temp_date_acquisition;
    }
    public String getDateAcquisition()
    {
        return date_acquisition;
    }

    public void setDescription(String temp_description)
    {
        description = temp_description;
    }
    public String getDescription()
    {
        return description;
    }

    public void setNumberOfCopies(int temp_num_copies)
    {
        num_copies = temp_num_copies;
    }
    public int getNumberOfCopies()
    {
        return num_copies;
    }

    public void setTitle(String temp_title)
    {
        title = temp_title;
    }
    public String getTitle()
    {
        return title;
    }
        public void addItem(int ids,String date,String desc,int num,String titles)
            {
                for(int h =0;h<=4;h++)
                {
                        items[h] = new Library();
                }                   
                if(xtr<=5)
                {   
                    items[xtr].setId(ids);
                    items[xtr].setDateAcquisition(date);
                    items[xtr].setDescription(desc);
                    items[xtr].setNumberOfCopies(num);
                    items[xtr].setTitle(titles);    
                    xtr++;  
                }
            }
        public void retrieveItem(int id)
                {
                    int z;
                    for(z=0;z<=4;z++)
                    {
                    if(id==items[z].getId())
                        {
                        JOptionPane.showMessageDialog(null, "ID number: " + items[z].getId() +
                                            "\nDate of Acquisition: " + items[z].getDateAcquisition() +
                                            "\nDescription: "+ items[z].getDescription() +
                                            "\nNumber of Copies: " + items[z].getNumberOfCopies() +
                                            "\nTitle: " + items[z].getTitle());         
                        }
                    }
                }
}

Here's the testing class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestLibrary
{
    static int xtr = 0;
    static Library[] book = new Library[5];
    public static void main(String[] args)
    {
        new TestLibrary();
        for(int h =0;h<=4;h++)
        {
                book[h] = new Library();
        }   
    }
    JFrame frame1 = new JFrame();
    JLabel title = new JLabel("Title:");
    JLabel desc = new JLabel("desc: ");
    JLabel num = new JLabel("Number of Copies:");
    JLabel id = new JLabel("Item ID: ");
    JLabel date = new JLabel("Date Of Acquisition: ");
    JButton store = new JButton("Add");
    JButton search = new JButton("Search");
    JTextField titleTF = new JTextField(10);
    JTextField descTF = new JTextField(10);
    JTextField numTF = new JTextField(10);
    JTextField idTF = new JTextField(10);
    JTextField dateTF = new JTextField(10);
    JTextField searchTF = new JTextField(10);
    JPanel panel1 = new JPanel(new FlowLayout());   
    TestLibrary()
    {
        frame1.setTitle("Library Catalog");
        frame1.setSize(300,400);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);

        frame1.add(panel1);
        panel1.add(id);
        panel1.add(idTF);
        panel1.add(desc);
        panel1.add(descTF);
        panel1.add(date);
        panel1.add(dateTF);
        panel1.add(title);
        panel1.add(titleTF);
            panel1.add(num);
        panel1.add(numTF);
        panel1.add(store);
        panel1.add(searchTF);
        panel1.add(search);
        store.addActionListener(new add());
        search.addActionListener(new retrieve());
        frame1.pack();
    }
    class add implements ActionListener
    {   
        public void actionPerformed(ActionEvent ae)
        {   
                String idSS = idTF.getText();
                int idS = Integer.parseInt(idSS);
                String dateS = dateTF.getText();
                int num = Integer.parseInt(numTF.getText());
                String title = titleTF.getText();
                String desc = descTF.getText();

                book[xtr].addItem(idS,dateS,desc,num,title);
                xtr++;
                idTF.setText("");
                dateTF.setText("");
                numTF.setText("");
                titleTF.setText("");
                descTF.setText("");
                JOptionPane.showMessageDialog(null,"Item Added!");
        }
    }
    class retrieve implements ActionListener
    {
        public void actionPerformed(ActionEvent ar)
        {
            int search = Integer.parseInt(searchTF.getText());

            for(int y=0;y<=4;y++)
            {
            book[y].retrieveItem(search);
            }   
        }
    }
}
0

what is my mistake on the line 3-6?

That's been answered more than once already. Maybe time for more thinking, coding, testing between questions?

0

What is the correct implementation here:

public void retrieveItem(int id)
                {
                    int z;
                    try
                    {
                    for(z=0;z<=counter;z++)
                    {
                    if(id==items[z].getId())
                        {
                        JOptionPane.showMessageDialog(null, "ID number: " + items[z].getId() +
                                            "\nDate of Acquisition: " + items[z].getDateAcquisition() +
                                            "\nDescription: "+ items[z].getDescription() +
                                            "\nNumber of Copies: " + items[z].getNumberOfCopies() +
                                            "\nTitle: " + items[z].getTitle()); 
                        }
                        }
                    }
                    catch(NullPointerException npe)
                        {
                            JOptionPane.showMessageDialog(null,"ID Not Found!","Error!",JOptionPane.ERROR_MESSAGE);
                        }
                }

Because every time i search for a correct id, the Message Dialog Box will show the correct infos, but when i click the "OK" button, the message in the catch block will appear. Where did i go wrong there?

0

print the stacktrace, it'll tell you way more then just a random "hey, you've hit null" message.
you may also want to break out of the loop once you've found the element you're looking for.

EDIT: ID Not found => that is the worst error message ever. that nullpointerexception has nothing to do with whether you have found the id or not (as you've said already)
you're just searching through an array in which there are still null values (non-initialized objects) present. any idea what happens if you call a method on a null?

Edited by stultuske

0

You don't show what code is executed when you press the OK button, so who knows?
One guess is that you have for(z=0;z<=counter;z++) so if there are 4 entries (ie counter = 4) you are accessing array elements 0-4 inclusive, ie 5 elements, of which the 5th will still be null.

In general, when yo uget a null pointer exception you should print the exception which will tell you teh exact line where it happened. Then you can print the values used on that line to see which is null, then you should be able to work out why.

0

Thankyou for the help. I appreaciated all of your criticism.
-THREAD CLOSED-

,br

1

just saying "Thread closed" won't stop people from reading/replying. just set the thread to - SOLVED.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.