public class Test {
public static void main(String[] args) {
Car c = new Car();
c.setInf("toyota", "red");
System.out.println("name: "+ c.brand + " colour: " + c.colour);
[code]....
Why do I get the result brand null, colour null? I know what null means but what am I missing here?
So I'm trying to add two numbers using a stack class I made myself. I have a main class with a main method in which I use three stacks on holds one number one holds the other and then I sum one digit at a time keeping in midn that there may be a carry before putting the result on a third stack.
My problem is I'm getting a null pointer error when it should be putting an integer into the top of my stack which is index 0, I've checked my code and can't see why it would be doing this.This is my main method in my main class
import java.util.*; import java.io.*; public class CS25driver { public static void main(String[]args) throws FileNotFoundException {
[code]...
my input file looks like a single line of input stating the problem followed by two lines of input which need to be summed.this is the error I'm getting
Exception in thread "main" java.lang.NullPointerException at StackClass.push(StackClass.java:38) at CS25driver.main(CS25driver.java:55)
public static javax.swing.JPanel pane; I initialize it in a method (Well, Netbeans' GUI builder does. :P) pane = new javax.swing.JPanel(){ @Override public void paint(Graphics g){ g.setColor(Color.BLACK); g.drawArc(8, 6, 15, 15, 0, 90); /* This works. The panel and frame are displayed, the arc is drawn, &c. */ } };
it works and all; the arc is drawn.I want to draw on it dynamically by instantiating its Graphics (I call the "public Graphics getGraphics()" method.). This has worked for me before, taking a JPanel's Graphics and drawing with it through a different method than public void paint(Graphics g); But when I do, it comes up with a NullPointerException. WAIT! Don't go rambling on about initializing the variable because I've gotten past that NPE newbie's blockade.public static void render(Something s) { /*This is in a different file altogether. pane is public, static so I can access it from here. That's not the problem.*/
Upon further investigation: Exception in thread "main" java.lang.NullPointerException at physics.Renderer.render(Renderer.java:30) <-- This line is "JPanel jp = Frame.pane;" at physics.Renderer.renderall(Renderer.java:24) <--This line calls public static void render(Something s) at physics.Updater.update(Updater.java:47) <--Renderer is just a tool used by the Updater. Sounds like a game loop, right? at physics.Physics.main(Physics.java:31) <-- The game loop.
it says that Line 30, making jp and pointing it to pane itself is the problem.
Conclusion: -The JPanel in the JFrame is created, initialized and overrides a parent method. -The JPanel is not initialized, meaning that it is null. There is obviously a problem.
I am working on a project which manages an airport's airplanes and flights based on user input. The method printFlights() - lines 133-134 - is returning null and I can't figure out why. The method is supposed to print information about each flight. The logic is identical to the printPlanes() method which is working successfully.
It is supposed to create an array of random strings and sort them according to their HTML numbers. It creates the array correctly and even compiles correctly, but has a null pointer exception when it comes to the sorting. (I have cut out other parts of the program that were irrelevant).
the lines that produces the null Pointer Exception is line 24. if(array[i].charAt(0)>array[maxIndex].charAt(0))...and 53.stringSelectionSort(stringArray);
import java.util.Arrays; import java.util.Random; /**tests the array.sort() subroutine vs. a 'selection sort' that I will write. a random list of integers is used first and times are recorded to determine which one is more efficient. A random list of strings is also tested.**/
When I try to getNextMailItem,it keeps returning null.Why?
public class MailServer { // Storage for the arbitrary number of mail items to be stored // on the server. private HashMap<String,ArrayList<MailItem>> mailbox; /** * Construct a mail server. */ public MailServer()
how to execute a particular class. I've created a Kinematics class that will execute and calculate all the functions pertaining to kinematic equations. In the main class, I will have the user provide the data in JTextField. Now I need to test which JTextfields (variables) were left empty. I plan to use a number of if/else statements :
if(distanceI != null && distanceF != null)
The problem with this is that these variables are double and therefore cannot be tested as null. I read a few things about Double but I really don't see how I'm supposed convert each double variable to Double, is that even possible?
public class Kinematics { double distanceI, distanceF, velocityI, velocityF, velocityAverage, acceleration; public Kinematics(){ //v2 = v1 + a*dt if(distanceI != null && distanceF != null){
In the case that vehicle.color is null what will happens? A null pointer error?
null will be concatenated to the string in vehicle.color place? or will not concatenate anything in the place of vehicle.color I'm asking that because I do not know if I have to check for null in each property of vehicle that I must concatenate (there are a lot of properties in the Vehicle object and some may not been set (they will return null).
The below method shows that a JOptionPane should display when the result is null, meaning it has not found what I was trying to look for. However only the if statement that finds the query runs:
public void find(Person person) { String query = "SELECT * from employee WHERE last LIKE'" + lastName + "'"; try { statement = connection.createStatement(); resultSet = statement.executeQuery(query);
I've been beavering away with Java for a few months. But as with all languages the String implementation looks designed to trip up even experienced programmers.
My current development gets data from various sources outside my control. When I get a string I want to test if it is empty/null/or whatever. Simple enough one thinks.
But if you search the internet you see everone seems to have a slightly different approach. So what is the best way of determining that a string is not useful to you?
I've had success with this
if(string == null || string.length() == 0)
But I've seen people using methods - not necessarily of String (e.g equals, empty) and regular expressions.
What is the best approach to this considering coding efficiency and/or processing efficiency (accepting you'd have to be processing a lot of strings for the latter to be an issue).
Any compact if statement or any type of test for checking if atleast ONE out of 4 integers is null. So 1 of all, 2 of all, 3 of all, or all of them being null will give a specific result ELSE do something else.
Is there a better way to remove null values from an array than what I have tried? This works just fine, but I just get the feeling that there is a better way to do this, without using the JCF.
private static String[] removeNullValues(String[] list){ int count = 0; for(int i = 0; i < list.length; i++){ if(list[i] == null) count++;
[Code] ....
I technically dont need to remove the null values for the project that I'm working on (since I just print it out and I can avoid null values with a simple statement like
Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
import java.text.DecimalFormat; import java.lang.Math; public class Student { //instantiate variables
Working on chapter 10 of my book which covers exceptions and file I/O. The only part that does not work is line 40 where it is supposed to print getMessage(). It print's null instead of the more verbose error message.
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class SalesReport2 { public static void main(String[] args) { String filename = "SalesData.txt"; int months = 0;
[Code] ....
temp.txt
123.33 asdf 549.85 dsaf 8456.23 21588.22 652.00
My output when using my test file.
ERROR: SalesData.txt does not exist. Enter another filename: temp.txt Nonnumeric data found in file: null The invalid record will be skipped. Nonnumeric data found in file: null The invalid record will be skipped. Number of months: 5 Total Sales: 31,369.63 Average Sales: 6,273.93
I then removed the try catch so that I could get a stacktrace
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class SalesReport2a { public static void main(String[] args)
[Code] ...
This was the output.
ERROR: SalesData.txt does not exist. Enter another filename: temp.txt Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at SalesReport2a.main(SalesReport2a.java:32)
I also pulled the copy of the code from the CD that came with the book and it's output is the same as my code.
The image in the book shows that the error should be more informative though.
what I get
Nonnumeric data found in file: null
vs
what I should get
Nonnumeric data found in file: For input string: "asdf"
My IDE has a visual editor for creating Swing applications. The created application windows have no layout manager and use method "setBounds()" for placing Swing components on the application window. I have built an application that uses JInternalFrame. Each JInternalFrame is a separate and different "screen" for the user to interact with.
There are around 2,000 screens in the application and new screens are constantly being added as well as existing screens being modified. As a result, the top-level container - a JFrame - is a fixed size and is not resizable. I have now been asked to increase the size of the JFrame while maintaining the proportions of the screens.
Rather than manually editing the invocations of "setBounds()" on all the screens, any alternative way to achieve this? Also, is there a way to achieve this such that if, in future, the JFrame will again be resized, the screesn will automatically adjust?
How should null pointer exception be avoided in a program. Should each and every place of access of a variable be checked for null and if null is there own customized exception should be thrown. Is this approach correct for avoiding null pointer exception.