I cannot get past this error. I had taken out the dataSort method which allowed it to compile, however I need the data to sort for my assignment. When it compiled the data was not passing through my constructors, I got no data output. Any assistance is greatly appreciated . I have to add on to the program this week for class but I did not get it working correctly and can't start this weeks work without it working properly =(
Exception in thread "main" java.lang.NullPointerException
at farmersMarket.product.dataSort(product.java:147)
at farmersMarket.product.addData(product.java:137)
at farmersMarket.product.addData(product.java:120)
at farmersMarket.product.main(product.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
package farmersMarket;
import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;
/**
*
* @author Ashey Anderson
*/
public class product {
// Create an array
private static DataFormat[] Data = new DataFormat[0]; //array object
// Keep track of current Data
private static int currentData = 0;
// main method begins execution of the Java application;
public static void main(String args[]) {
//loading the array object
addData("Fresh Farm","Lemon",43567809, 8, 10.78);
addData("Dale Farm","Lettuce",98561278, 6, 8.99);
addData("Star Farm","Tomato", 37656901, 9, 12.95);
addData("Apple Acers","Apple", 30065489, 12, 12.50);
addData("Orangeland Farms","Orange", 67085129,1,14.99);
dataSort();
ApplicationGUI appGUI = new ApplicationGUI(); //calls constructor
} // end main
public static float inventoryValue() {
float total = 0;
for (int x = 0; x < Data.length; x++) {
total += Data[x].getiValue();
}// end for loop
return total;
}
// Return a string for the current item
public static String getCurrentString() {
if (Data.length == 0) {
return "No data";
} else {
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Build Inventory String
formatter.format(Data[currentData].toString());
formatter.format("\n");
formatter.format("Total Product Value: %s\n", NumberFormat.getCurrencyInstance().format(inventoryValue()));
return sb.toString();
}
}
// Method to increment current item
public static void showNext() {
if (currentData == Data.length - 1) {
showFirst();
} else {
currentData++;
}
}
// Method to go to first item
public static void showFirst() {
currentData = 0;
}
// Method to go to last item
public static void showLast() {
currentData = Data.length - 1;
}
// array of string
public static String[] getCurrentStringArray() {
String[] currentStringArray = new String[5];
currentStringArray[0] = Data[currentData].getiName();
currentStringArray[1] = Data[currentData].getDept();
currentStringArray[2] = Integer.toString(Data[currentData].getiSku());
currentStringArray[3] = Integer.toString(Data[currentData].getiSold());//convert items to string
currentStringArray[4] = Double.toString(Data[currentData].getiPrice());
return currentStringArray;
}
//assigns the Sku number to the current array object
public static int getNextData() {
int newCode = 0;
for (int x = 0; x < Data.length; x++) {
if (Data[x].getiSku() > newCode) {
newCode = Data[x].getiSku();
}
}
return newCode + 1;
}
public static void addData(String iName, String Dept,int iSku, int iSold, double iPrice) {
String[] newData = new String[5]; //create a String array
//load the array with the items that passed into the method
newData[0] = iName;
newData[1] = Dept;
newData[2] = Integer.toString(iSku);
newData[3] = Integer.toString(iSold); //convert the integer pass in to a string
newData[4] = Double.toString(iPrice);
addData(newData); //method call
//return;
}
// New Method called addData
public static void addData(String[] data) {
// Add a new Salesman
DataFormat iDataFormat = new DataFormat(getNextData(), data[0], data[1], Integer.valueOf(data[3]), Double.valueOf(data[4]));
Class elementType = Data.getClass().getComponentType();
DataFormat[] newArray = (DataFormat[]) java.lang.reflect.Array.newInstance(elementType, Data.length + 1);
System.arraycopy(Data, 0, newArray, 0, Data.length);
newArray[newArray.length - 1] = iDataFormat;
Data = newArray;
showLast();
dataSort();
}
private static void dataSort() {
DataFormat tmp;
for (int i = 0; currentData < Data.length; currentData++){
String s1 = Data[currentData].getiName();
String s2 = Data[i].getiName();
if (s1.compareTo(s2) > 0){
tmp = Data[currentData];
Data[currentData] = Data[i];
Data[i] = tmp;
}
}
}
}
package farmersMarket;
/**
*
* @author Ashley Anderson
*/
class Data {
private String iName;
private String Dept;
private int iSku;
private double iPrice;
private int iSold;
public Data(int iSku, String iName, String Dept, int iSold, double iPrice) {
iName = iName;
Dept = Dept;
iSku = iSku;
iSold = iSold;
iPrice = iPrice;
}
public Data(String iName){
Dept = Dept;
iName = iName;
iSku = 0;
iSold = 0;
iPrice = 0;
}
public Data() {
iName = "";
Dept = "";
iSku = 0;
iSold = 0;
iPrice = 0;
}
/**
* @return the Dept
*/
public String getDept(){
return Dept;
}
/**
* @param Dept the Dept to set
*/
public void setDept(String Dept){
this.Dept = Dept;
}
/**
* @return the iSku
*/
public int getiSku() {
return iSku;
}
/**
* @param iSku the iSku to set
*/
public void setiSku(int iSku) {
this.iSku = iSku;
}
/**
* @return the iName
*/
public String getiName() {
return iName;
}
/**
* @param iName the iName to set
*/
public void setiName(String iName) {
this.iName = iName;
}
/**
* @return the iPrice
*/
public double getiPrice() {
return iPrice;
}
/**
* @param iPrice the iPrice to set
*/
public void setiPrice(double iPrice) {
this.iPrice = iPrice;
}
/**
* @return the iSold
*/
public int getiSold() {
return iSold;
}
/**
* @param iSold the iSold to set
*/
public void setiSold(int iSold) {
this.iSold = iSold;
}
double getiValue() {
return getiPrice()*getiSold();
}
package farmersMarket;
import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;
/**
*
* @author esc1vbr
*/
class DataFormat extends Data{
private String Department;
public DataFormat(int iSku,String Dept, String iName, int iSold, double iPrice ){
super (iSku, iName , Dept, iSold, iPrice );
Department = Dept;
}
public DataFormat(String iName) {
super(iName);
}
public String getDepartment(){
return Department;
}
public void setDepartment(String dept){
Department = dept;
}
public double getiValue(){
return super.getiValue();
}
public double getSalesTax() {
return super.getiValue()*.07;
}
public String toString() {
StringBuilder sb = new StringBuilder();
// Send all output to the Appendable object sb
Formatter formatter = new Formatter(sb, Locale.US);
// Explicit argument indices may be used to re-order output.
formatter.format("Item Number: %d\n", getiSku());
formatter.format("Item Name: %s\n", getiName());
formatter.format("Farm Name: %s\n", getDept());
formatter.format("Number of Items Sold: %d\n", getiSold());
formatter.format("Item Price: %s\n", NumberFormat.getCurrencyInstance().format(getiPrice()));
formatter.format("Total Sales: %s\n", NumberFormat.getCurrencyInstance().format(getiValue()));
formatter.format("Sales Tax: %s\n", NumberFormat.getCurrencyInstance().format(getSalesTax()));
formatter.format("Total value plus Tax: %s\n", NumberFormat.getCurrencyInstance().format(getiValue() + getSalesTax()));
return sb.toString();
}
}
package farmersMarket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
*
* @author Ashley Anderson
*/
class ApplicationGUI implements ActionListener
{
static JFrame jfrMain;
// Declare 4 buttons
static JButton jbnNext;
// Declare a panel to draw on - holds labels
static JPanel jplNavigation;
// Declare Advanced panel for additional buttons - holds buttons
static JPanel jplAdvanced;
// Declare a text area
static JTextArea jtxtArea;
// Declare a Label
static JLabel jlbLabel;
public ApplicationGUI(){ //constructor
// Create and set up the window.
jfrMain = new JFrame("Sales Receipt");
jfrMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the text area
jtxtArea = new JTextArea();
jtxtArea.append(product.getCurrentString());
jfrMain.getContentPane().add(jtxtArea);
// Create Panel for label
jplNavigation = new JPanel();
jlbLabel = new JLabel();
jlbLabel.setText("Ander Farms");
// Create Advanced panel for additional buttons
jplAdvanced = new JPanel();
// Create Next button
jbnNext = new JButton("Next");
jbnNext.addActionListener(this);
jbnNext.setActionCommand("next");
// Add Title label to the panel
jplNavigation.add(jlbLabel);
// Add buttons to Advanced Panel
jplAdvanced.add(jbnNext);
// Add panel to frame
jfrMain.getContentPane().add(jplNavigation, BorderLayout.PAGE_START);
jfrMain.getContentPane().add(jtxtArea, BorderLayout.WEST);
// Add Advanced panel to frame
jfrMain.getContentPane().add(jplAdvanced, BorderLayout.PAGE_END);
//Display the window.
jfrMain.pack();
jfrMain.setVisible(true);
}
private static void showCurrentData(){
jtxtArea.setText(product.getCurrentString());
}
public static void enableButtons(boolean enable) {
jbnNext.setEnabled(enable);
//return;
}
@Override
public void actionPerformed(ActionEvent e) {
// Auto-generated method stub
if ("next".equalsIgnoreCase(e.getActionCommand())) {
product.showNext();
showCurrentData();
}
}
}