PDA

View Full Version : Beginner Java getting error messages I don't understand


Spiritclimb2003
02-06-2010, 03:20 AM
For my Beginning Java homework, I am trying to compute Net Pay from Gross Pay and then send it out to a file. I have to deduct state tax, federal tax, etc. When I completed it and ran it through my compiler I got this error message 10 times:

non-static variable outFile cannot be referenced from a static context
outFile.printf("-31s%1s%8.2f%n", "Gross Amount:", "$", grossAmt);


each line is slightly different in the computations, but basically the same. Not sure what the error message means and what I did wrong. This is my first computer language class. Anybody might be able to help? I would really appreciate it.

Thanks.

UrbanTwitch
02-06-2010, 04:51 AM
It would be great if you could post the WHOLE code so we may help you more efficiently.

Spiritclimb2003
02-06-2010, 05:44 AM
Do you need me to post the whole program? I'm not sure how much to post. I know this is a general project for many new learners like myself and I didn't want to post too much of it.

I've been working with it since the first post. Now I just get two error messages that say the variable str and name might not have been initialized. I have a feeling once I get that fixed, I'll be back to the first problem, but not sure.


import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;

public class Paycheck
{


public static void main (String[] args) throws
FileNotFoundException
{
//declare variables
String str;
String inputStr;
String name;

PrintWriter outFile = new PrintWriter ("c:\\src");

double grossAmt = Double.parseDouble(str);
double netPay = Double.parseDouble(str);
double fedTax = Double.parseDouble(str);
double stateTax = Double.parseDouble(str);
double ssTax = Double.parseDouble(str);
double medTax = Double.parseDouble(str);
double pensionPlan = Double.parseDouble(str);
double healthIns = Double.parseDouble(str);

//display input boxes
inputStr = JOptionPane.showInputDialog
("Enter your first and last name and press OK");
inputStr = JOptionPane.showInputDialog
("Enter your gross salary and press OK");

//computations for deductions and Net Pay
fedTax = grossAmt * .15;
stateTax = grossAmt * .035;
ssTax = grossAmt * .0575;
medTax = grossAmt * .0275;
pensionPlan = grossAmt * .05;
healthIns = 75;
netPay = grossAmt - fedTax - stateTax - ssTax - medTax - pensionPlan - healthIns;

//Output statements
outFile.println(name);
outFile.printf("-31s%1s%8.2f%n", "Gross Amount:", "$", grossAmt);
outFile.printf("-31s%1s%8.2f%n", "Federal Tax:", "$", fedTax);
outFile.printf("-31s%1s%8.2f%n", "State Tax:", "$", stateTax);
outFile.printf("-31s%1s%8.2f%n", "Social Security Tax:", "$", ssTax);
outFile.printf("-31s%1s%8.2f%n", "Medicare/Medicaid Tax:", "$", medTax);
outFile.printf("-31s%1s%8.2f%n", "Pension Plan:", "$", pensionPlan);
outFile.printf("-31s%1s%8.2f%n", "Health Insurance:", "$", healthIns);
outFile.printf("-31s%1s%8.2f%n", "Net Pay:", "$", netPay);

//close file and exit out of Input Dialog boxes
outFile.close();
System.exit(0);
}
}

UrbanTwitch
02-06-2010, 04:00 PM
In Java, class instance variables and static variables have default values: null for all object types, false for boolean and 0 for numerics. But local variables inside a method have no defaults. So you're just declaring them and then you get that error message.

Delete the variables of str, inputStr, and name. Go under
public class Paycheck
{


and add:
public static String str;
public static String name;
public static String inputStr;

This makes is so the variable is publicly available to every method and is static. Static means there is only one copy per class, regardless of how many objects are created from it. They are stored in static memory.

Spiritclimb2003
02-06-2010, 05:44 PM
Thank you very much for taking the time to help me.