Results 1 to 5 of 5
- 07-09-2012, 11:52 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Getcomponents NullPointerException
Hello everyone,
I posted about this problem on another Java forum but I didn't get a response. I am currently working on a project and I am trying to figure out how to properly utilize either JExcelAPI or Apache POI in order to write an excel sheet. I have a JPanel (on a JFrame) with user-added JTextAreas from a menu click. The user can then right click on these text areas and change their text as well as attributes that are defined by the user. I want this program to write an excel sheet that lists "object name, attribute name, sequence number, attribute value" in different rows. So each row will correspond to a different attribute of an object. Since there is a lot to this I want to focus on just one aspect now that may help me understand how to complete this.
Right now I just want to write the string values of the textareas into cells in the spreadsheet. I have been using JExcelAPI but so far I have only been able to make the program write a sheet that has the labels of the different columns. I was trying to use the getcomponents() method in order to create an array of the components that are currently visible on the jpanel (the user can 'delete' an object by setting it to not visible). But when I make the array I can't reference the individual components and use the gettext method as I was intending so I have no idea how I can make this work.
Exception in thread "main" java.lang.NullPointerException
Java Code://The first line below has the error JTextArea[] components = (JTextArea[]) jPanel2.getComponents(); JTextArea component = null; for (int i = 0; i < components.length; i++) { component = components[i]; if (component.isVisible()) { String component0text = components[0].getText(); jxl.write.Label jjjj = new jxl.write.Label(0, 1, component0text, cf); s.addCell(jjjj); } }
Here is the code in context. It is at the very bottom. The upper part is where the components are created.
Java Code:JPopupMenu Pmenu; JMenuItem menuItem; int counter = 0; JTextArea objectclicked; ComponentMover cm = new ComponentMover(); AttributeDialog ad = new AttributeDialog(); JTextArea[] object = new JTextArea[100]; private void objectinsertActionPerformed(java.awt.event.ActionEvent evt) { //objects for(int i = 0; i < object.length; ++i) { object[i] = new JTextArea("Object " + (i)); object[i].setEditable(false); object[i].addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent Me){ if(Me.isPopupTrigger()){ Pmenu.show(Me.getComponent(), Me.getX(), Me.getY()); objectclicked = (JTextArea) Me.getComponent(); } } }); } //Create object on jpanel counter++; jPanel2.add(object[counter]); jPanel2.revalidate(); jPanel2.repaint(); //Drag and drop cm.registerComponent(object[counter]); //Right click menu for objects Pmenu = new JPopupMenu(); JMenuItem editobject = new JMenuItem("Edit name"); Pmenu.add(editobject); JMenuItem deleteobject = new JMenuItem("Delete"); Pmenu.add(deleteobject); JMenuItem addattribute = new JMenuItem("Add attributes"); Pmenu.add(addattribute); //right click functions //edit name editobject.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ while (true) { String a = JOptionPane.showInputDialog(null, "Edit object name"); if(a == null){ break; } else{ objectclicked.setText(a); break; } } } }); //delete object deleteobject.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ objectclicked.setVisible(false); } }); //add attributes via dialog addattribute.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ ad.setVisible(true); } }); public static void main(String args[]) throws IOException, WriteException { JPopupMenu p = new JPopupMenu(); // Writing Data to ExcelSheet String filename = "input1.xls"; WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(new Locale("en", "EN")); WritableWorkbook workbook = Workbook.createWorkbook(new File( filename), ws); WritableSheet s = workbook.createSheet("DataSheet", 0); System.out.println("In writeDataSheet() Method"); /* Format the Font */ WritableFont wf = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD); WritableCellFormat cf = new WritableCellFormat(wf); cf.setWrap(true); /* Creates Label and writes date to one cell of sheet */ jxl.write.Label one = new jxl.write.Label(0, 0, "Object", cf); s.addCell(one); WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT9); jxl.write.Label two = new jxl.write.Label(1, 0, "Attribute", cf); s.addCell(two); jxl.write.Label three = new jxl.write.Label(2, 0, "Sequence", cf); s.addCell(three); jxl.write.Label four = new jxl.write.Label(3, 0, "Initial Value", cf); s.addCell(four); JTextArea[] components = (JTextArea[]) jPanel2.getComponents(); JTextArea component = null; for (int i = 0; i < components.length; i++) { component = components[i]; if (component.isVisible()) { String component0text = components[0].getText(); jxl.write.Label jjjj = new jxl.write.Label(0, 1, component0text, cf); s.addCell(jjjj); } } workbook.write(); workbook.close();
- 07-10-2012, 01:08 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Getcomponents NullPointerException
A NullPointerException occurs when you use a variable or other expression as if it had a non null value when its value is really null. (Variables will be null until you assign them a value.) In this case, the only thing being used as if it had a non null value is jPanel2. You can test whether it is null by adding a line of code:Java Code:JTextArea[] components = (JTextArea[]) jPanel2.getComponents();
If it is, in fact, this variable which is null you should look in your code to where you thought you assigned it a value and figure out why that didn't happen. Nothing in the extract of code you posted showed jPanel2 being assigned a value.Java Code:System.out.println("About to call getComponents jPanel2=" + jPanel2); JTextArea[] components = (JTextArea[]) jPanel2.getComponents();
- 07-11-2012, 02:36 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Re: Getcomponents NullPointerException
Thanks for your reply. I suppose it was returning null because I had the jpanel declared as static. When I do not have it declared as static I get a different error on the same line of code that says "Uncompilable source code - non-static variable jPanel2 cannot be referenced from a static context"
I have been searching google for help but I can't figure out what makes this a static context or how I can fix this. Thanks again.
-
Re: Getcomponents NullPointerException
It means you're trying to run code that can't compile -- never do this!
To fix it don't try to access non-static variables or methods from a static method. Instead create an instance of the class and call the method off of the class.
- 07-11-2012, 03:14 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Getcomponents NullPointerException
Consider - seriously - not using a static variable. It is likely at the source of the problem.I had the jpanel declared as static
If you can't understand the compiler messages, post them along with the code to which they refer. Posting complete code is always a good thing. But so is code that is brief! You might have to construct a brief example showing how the panel is to be used free from all the Apache stuff which has nothing to do with the actual problem.
-----
The main() method is a "static" context. Where "static" c== "having no object associated with it". From a static context you can only call static methods, access static variables etc.
Similar Threads
-
NullPointerException
By JG4m3r in forum New To JavaReplies: 2Last Post: 05-16-2012, 06:36 PM -
NullPointerException
By s0meb0dy in forum New To JavaReplies: 3Last Post: 10-09-2010, 08:12 PM -
Using JComponent methods on a JComponent that was found using .getComponents()
By tashimoto in forum New To JavaReplies: 2Last Post: 10-01-2010, 07:18 PM -
NullPointerException
By sanu in forum Java AppletsReplies: 3Last Post: 08-21-2010, 08:37 PM -
NullPointerException
By speedzojie@gmail.com in forum New To JavaReplies: 5Last Post: 06-03-2010, 05:39 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks