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 18:25  |
Eclipse User |
|
|
|
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 519 times)
|
|
| | |
Goto Forum:
Current Time: Wed Mar 19 16:14:57 EDT 2025
Powered by FUDForum. Page generated in 0.09272 seconds
|