Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Exception in thread "main" java.lang.NullPointerException(Eclipse SWT Exception in thread "main" java.lang.NullPointerException)
Exception in thread "main" java.lang.NullPointerException [message #1767667] Sat, 08 July 2017 22:25 Go to next message
raphael juwe is currently offline raphael juweFriend
Messages: 1
Registered: July 2017
Junior Member
Good day community.


Am writting a eclipse.swt.SWT application. Lately i had been experiencing the problem :


Exception in thread "main" java.lang.NullPointerException
at demo.JFrameProduct.FillData(JFrameProduct.java:56)
at demo.JFrameProduct.open(JFrameProduct.java:77)
at demo.JFrameProduct.main(JFrameProduct.java:42).

I had referenced SWT_LIB in my eclipse reference library.

My code is below :

package demo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import java.util.*;

import javax.swing.JOptionPane;

import entity.*;
import model.*;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


public class JFrameProduct {

protected Shell shlProductApp;

private Table tableProduct;
private Text textId;
private Text textPrice;
private Text textQuantity;
private Text textDescription;

private ProductModel pm;
private Text textName;


/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
JFrameProduct window = new JFrameProduct();
window.open();
try {


} catch (Exception e) {
e.printStackTrace();
}
}


private void FillData(){

ProductModel pm = new ProductModel();
tableProduct.removeAll();
for (Product p : pm.findAll()) {
TableItem tableItem = new TableItem(tableProduct, SWT.NONE);
tableItem.setText(new String[] {String.valueOf(p.getId()),
p.getName(), String.valueOf(p.getPrice()),
String.valueOf(p.getQuantity())});


}




}


/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
FillData();
shlProductApp.open();
shlProductApp.layout();
while (!shlProductApp.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}


FillData();
}

/**
* Create contents of the window.
*/
protected void createContents() {
shlProductApp = new Shell();
shlProductApp.setSize(450, 409);
shlProductApp.setText("Product App");

tableProduct = new Table(shlProductApp, SWT.BORDER | SWT.FULL_SELECTION);
tableProduct.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TableItem[] selection = tableProduct.getSelection();
int id = Integer.parseInt(selection[0].getText());
Product p = pm.find(id);
textDescription.setText(p.getDescription());
textId.setText(String.valueOf(p.getId()));
textName.setText(p.getName());
textPrice.setText(String.valueOf(p.getPrice()));
textQuantity.setText(String.valueOf(p.getQuantity()));


}
});
tableProduct.setBounds(22, 10, 402, 124);
tableProduct.setHeaderVisible(true);
tableProduct.setLinesVisible(true);

TableColumn tblclmnId = new TableColumn(tableProduct, SWT.NONE);
tblclmnId.setWidth(100);
tblclmnId.setText("Id");

TableColumn tblclmnName = new TableColumn(tableProduct, SWT.NONE);
tblclmnName.setWidth(100);
tblclmnName.setText("Name");

TableColumn tblclmnPrice = new TableColumn(tableProduct, SWT.NONE);
tblclmnPrice.setWidth(100);
tblclmnPrice.setText("Price");

TableColumn tblclmnQuantity = new TableColumn(tableProduct, SWT.NONE);
tblclmnQuantity.setWidth(100);
tblclmnQuantity.setText("Quantity");

Label lblId = new Label(shlProductApp, SWT.NONE);
lblId.setBounds(6, 155, 55, 15);
lblId.setText("Id");

textId = new Text(shlProductApp, SWT.BORDER);
textId.setBounds(100, 155, 162, 21);

Label lblName = new Label(shlProductApp, SWT.NONE);
lblName.setText("Name");
lblName.setBounds(6, 181, 55, 15);

textName = new Text(shlProductApp, SWT.BORDER);
textName.setBounds(100, 181, 162, 21);

Label lblPrice = new Label(shlProductApp, SWT.NONE);
lblPrice.setText("Price");
lblPrice.setBounds(6, 216, 55, 15);

textPrice = new Text(shlProductApp, SWT.BORDER);
textPrice.setBounds(100, 216, 162, 21);

Label lblQuantity = new Label(shlProductApp, SWT.NONE);
lblQuantity.setText("Quantity");
lblQuantity.setBounds(6, 250, 55, 15);

textQuantity = new Text(shlProductApp, SWT.BORDER);
textQuantity.setBounds(100, 250, 162, 21);

Label lblDescription = new Label(shlProductApp, SWT.NONE);
lblDescription.setText("Description");
lblDescription.setBounds(6, 296, 74, 15);

textDescription = new Text(shlProductApp, SWT.BORDER);
textDescription.setBounds(100, 299, 162, 21);

Button btnSave = new Button(shlProductApp, SWT.NONE);
btnSave.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

Product p = new Product();
p.setDescription(textDescription.getText());
p.setName(textName.getText());
p.setPrice(Double.parseDouble(textPrice.getText()));
p.setQuantity(Integer.parseInt(textQuantity.getText()));
if(pm.create(p)) {
JOptionPane.showMessageDialog(null,"Add new product successfull");
FillData();
}

else
JOptionPane.showMessageDialog(null,"Add new product failed");
FillData();


}
});
btnSave.setBounds(100, 336, 75, 25);
btnSave.setText("Save");

textName = new Text(shlProductApp, SWT.BORDER);
textName.setBounds(100, 189, 162, 21);

Button btnDelete = new Button(shlProductApp, SWT.NONE);
btnDelete.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

int result = JOptionPane.showConfirmDialog(null,"Are you sure?", "Confirm", JOptionPane.YES_NO_OPTION);

if(result == JOptionPane.YES_OPTION) {
TableItem[] selection = tableProduct.getSelection();
int id = Integer.parseInt(selection[0].getText());
Product p = pm.find(id);

pm.delete(p);
FillData();
}
}
});
btnDelete.setBounds(187, 336, 75, 25);
btnDelete.setText("Delete");




}
}


Your input in resolving this issue is needed.

Thank you.





  • Attachment: Capture.PNG
    (Size: 44.53KB, Downloaded 144 times)
Re: Exception in thread "main" java.lang.NullPointerException [message #1767670 is a reply to message #1767667] Sun, 09 July 2017 03:26 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1436
Registered: July 2009
Senior Member
You've got a null object reference in you code that you are asking the VM to de-reference some where in line 56 of your JFrameProject.java file. You will need to figure out what reference is null and then add a mechanism to make sure that it is not null before you de-reference it.

I would suggest using the debugger to step through the code to figure out what reference is negative.

Getting help on debugging null pointer references is better pursued in a general Java programming forum such as Java Ranch or Stack Overflow. This forum is for answering questions about using Eclipse tools not general Java programming questions.
Re: Exception in thread "main" java.lang.NullPointerException [message #1767675 is a reply to message #1767670] Sun, 09 July 2017 08:47 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 3596
Registered: July 2009
Senior Member

The user guide section on catching exceptions might help.

Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:Eclipse Mars is not debugging. Please help
Next Topic:Can't open Memory Analyzer when modify the Xmx value
Goto Forum:
  


Current Time: Sat Feb 29 04:33:02 GMT 2020

Powered by FUDForum. Page generated in 0.02677 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top