Swing/AWT/SWT :: GetGraphics Method Returns Null
Aug 23, 2007
The getGraphics() method in my constructor is returning a nullPointerException and I can't figure out why. This class is extending a JPanel and I thought calling super() would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics() method is returning null, so the next line that calls the graphics object is actually the one that produces the error.
int leftMargin = 2, rightMargin = 2, topMargin = 2, bottomMargin = 2;
int lineSpacing = 1;
int currentX = leftMargin, currentY = topMargin;
Font defaultFont, customFont;
public HTMLFrame(){
super();
[Code] ....
print(String) is a method I made that draws lines of text on the graphics object, similar to System.out.println(String) in the command prompt.
View Replies
ADVERTISEMENT
Nov 23, 2014
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"
Not sure why it isn't working.
View Replies
View Related
Oct 4, 2014
I have embed a query in the JAVA to retrieve the data from the back end and I use Oracle for back end. What I'm trying to do is in my query has been shown below. The issue here is that result set returns the NULL. But fine when I change the query "SELECT 5 FROM DUAL" and it returns 5 for me which right. When I try to run the same at the back end as a query I'm getting proper result.
public String getReturnPart(String partNumberIn) {
//int returnPartNumber = 0;
String query =
"SELECT COUNT(*) AS CHECK1 FROM PART_MASTER PM,UNITS_OF_MEASURE UOM WHERE PM.PART_NUMBER = '" + partNumberIn
+ "' AND PM.UNIT_OF_ISSUE = UOM.UNIT_OF_MEASURE AND UOM.UNIT_TYPE = 0 ";
//String query = "SELECT 5 AS CHECK1 FROM DUAL"; This returns fine as "5"
ResultSet result = null;
[Code] ....
View Replies
View Related
Mar 31, 2014
I'm using NetBeans to create this project and I have the gui registering text boxes, but it won't retrieve what the user entered, so the calculate button returns a null exception error.
Java Code:
package payroll;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
[Code] .....
View Replies
View Related
Oct 6, 2014
In Oracle SQL, when you do trim(column_name), if column_name is blank spaces only (say datatype is CHAR(8)), then "trim(column_name) is null" is true.
In JPQL, if you do "TRIM ( BOTH FROM p.column_name ) is null", does it evaluate to true just like in Oracle SQL?
The reason I'm asking, is my colleague wrote some code in JPQL like below:
... TRIM ( BOTH FROM CONCAT(p.column_name, '#@') ) = '#@'
He said he originally wanted to compare the TRIM result to empty String '', but the result is false, so he concat the column to some junk.
So if after the TRIM, the result is the same junk he added, then the column contains only blanks. I think this works but there could a simpler way to do it.
View Replies
View Related
May 27, 2014
I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.
Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.
import java.util.Scanner;
public class censorProgram {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println ("How many words would you like to enter?");
int lineOfText = input.nextInt();
[Code] ....
I have to make new string array in the method and return words without four letters in the main method
View Replies
View Related
Mar 4, 2014
I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.
I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;
import java.util.Arrays;
public class hhhh {
public static void main(String[] args) {
double [] a = {1,2,3};
double [] b = {2,3,4};
[code]...
View Replies
View Related
Jan 26, 2015
Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.
Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.
package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list
[Code]...
View Replies
View Related
Jan 29, 2015
My isEmpty method only returns false. Is something wrong? I printed the empty and not empty for testing purposes.
//determines if there are any items in the queue
public boolean isEmpty() {
if (front == -1 && rear == -1) {
System.out.println("empty");
return true;
} else {
System.out.println("not empty");
return false
}
}
View Replies
View Related
Sep 30, 2014
I am doing a homework assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding. If someone could dumb down their response and tell me what I did wrong
This is what I supposed to do:
1.) Type the following shell for the class:
public class ParadiseInfo
{
}
2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:
public static void main(String[] args)
{
}
3.) Between the braces of the main()Method, insert a call to the displayInfo() method:
displayInfo();
4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:
public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}
This is what I attempted to do: I know it is wrong I am just clueless on where to put the code and why
public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}
--- Update ---
I have also tried this:
public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{
[Code]...
The very last attempt I only ended up with one error which is:
G:ParadiseInfo.java:3: error: invalid method declaration; return type required
displayInfo();
^
1 error
Tool completed with exit code 1
Java is very new to me and I have no clue on what I am doing wrong.
View Replies
View Related
Oct 12, 2014
I am trying to use method calls with returns but it keeps on showing errors. The errors say class, interface, or enum expected. I realize this error occurs if there is issue with declaring class - but i can't seem to find the error. I will post the code that shows error.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FuelCost extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);
[Code] ....
The error states:
C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:290: error: ';' expected
public double calculateCostToFillTank()
^
C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:292: error: class, interface, or enum expected
return (averageMilesPerTank / milesPerGallonRating) * currentPricePerGallon;
[Code] ....
12 errors
Tool completed with exit code 1
View Replies
View Related
Dec 10, 2014
I'm taking a class in object oriented programming and we have a task to write a method that returns positive, negative or zero depending on the sum of two variables.
I've had a go at it and i've got to a certain point but i'm struggling to get past this on error - return outside method.
public class Test {
public int sumArgs;
public int arg1;
public int arg2;
[Code] ....
The error occurs on the line 'return "Positive";
View Replies
View Related
Feb 23, 2014
I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.
import acm.program.*;
public class MethodsThatReturnNonNumericValues extends ConsoleProgram {
public void run() {
int month=readInt("Enter month number");
int day=readInt("Enter day");
int year=readInt("Enter year");
[Code] ....
View Replies
View Related
Apr 4, 2015
This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:
public static Object max(java.lang.Comparable[] a)
All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.
Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.
Name your java class Max and your java file Max.java.
I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?
public class Max implements Comparable {
public static Object max(java.lang.Comparable[] a) {
java.lang.Comparable tempObj = null;
for (int i = 0; i < a.length; i++) {
if (a[i].compareTo(tempObj) > 0)
[Code] ....
View Replies
View Related
Oct 25, 2014
I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.
View Replies
View Related
Sep 28, 2014
I am doing an assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding.
This is what I supposed to do:
1.) Type the following shell for the class:
public class ParadiseInfo
{
}
2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:
public static void main(String[] args)
{
}
3.) Between the braces of the main()Method, insert a call to the displayInfo() method:
displayInfo();
4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:
public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}
This is what I attempted to do:
I know it is wrong I am just clueless on where to put the code and why
public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}
--- Update ---
I also tried it this one and ended up with 1 error..
public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}
}
View Replies
View Related
Jun 28, 2014
Write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.
View Replies
View Related
Jun 22, 2014
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...
This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:
package com.pathtocreating.Breakout_V2.state;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
public class StateManager {
private ArrayList<State> states;
[Code] ....
The code that calls the change of state in MainMenuState is called in the update method:
if(inputController.isMouseClicked("LeftClick")) {
if(playHover) {
soundManager.playSound("ButtonClick");
BreakoutMain.sm.changeStates("GameState");
[Code] ....
Here is the full stack trace when clicking the area to change states:
Exception in thread "Thread-2" java.lang.NullPointerException
at sun.java2d.SunGraphics2D.validateColor(Unknown Source)
at sun.java2d.SunGraphics2D.<init>(Unknown Source)
at sun.awt.image.SunVolatileImage.createGraphics(Unknown Source)
[Code] ...
It gives that same value both before and after clicking on the region.
Printing out b after the State is changed to GameState gives the following:
java.awt.Component$BltSubRegionBufferStrategy@66116674
I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.
View Replies
View Related
Sep 25, 2014
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.
View Replies
View Related
May 18, 2015
I am trying to return an array and I keep getting a null error. The below class sets the material numbers into an array and should return that array if called :
public class Jobs {
private int[] materialsNumber;
//change to parts and create another class that gets the materials for the parts
public int[] job1() {
materialsNumber[0] = 11960120;
[Code] ....
I later try to call the method. The program executes but stops after I println "test in loop"
public class PurchaseOrdersToParts {
private Jobs job = new Jobs();
int[] getPartsForPurchaseOrder(BigDecimal purchaseOrder) {
System.out.println("inside getparts");
BigDecimal testNum = new BigDecimal(123.0);
[Code] ....
This is the method that is calling the method in the GenerateOrdersToParts class
private PurchaseOrdersToParts purchaseOrdersToParts = new PurchaseOrdersToParts();
@Inject
PoRepository poRepository;
public GenerateShopJobTickets() {
[Code] .....
View Replies
View Related
Mar 19, 2015
I'm trying to call the grade.processFile method from the main method but I'm getting this Error below. I'll post my code which includes the main method and the class underneath the error message:
Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.jav a:130)
at java.util.Scanner.<init>(Scanner.java:611)
at MyGrades.processFile(MyGrades.java:49)
at myGradesMain.main(myGradesMain.java:19)
import java.util.Scanner;
import java.io.*;
[code]...
View Replies
View Related
Apr 11, 2015
ServiceUIFactory always returns null for MAIN_UIROLE
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
ServiceUIFactory factory = defaultPrintService.getServiceUIFactory();
Object swingui = factory.getUI(ServiceUIFactory.MAIN_UIROLE, ServiceUIFactory.JCOMPONENT_UI);
System.out.println(swingui);
Object jdialogui = factory.getUI(ServiceUIFactory.MAIN_UIROLE, ServiceUIFactory.JDIALOG_UI);
System.out.println(jdialogui);
[code]...
View Replies
View Related
Aug 10, 2014
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?
View Replies
View Related
Mar 31, 2014
I am very Java illiterate and I need to know how would i set all the variables in the first class to null or 0 to make a new method to clear everything.
import java.util.Arrays;
import java.util.Scanner;
public class studentMethods
{
private double totalScore;
private int count;
[Code] ....
View Replies
View Related
Jun 15, 2014
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");
[code]....
View Replies
View Related
Dec 16, 2014
i am trying to write a class method which will take in a string and returns a string which is the reversed version of that string. it compiles fine but when i try to run it it states Main method not found in class StringReverse,please define the main method as public static void main(String[]args). I am new to java and cannot figure out
import javax.swing.JOptionPane;
public class StringReverse {
public String reverseString(String str){
JOptionPane.showInputDialog(null,"Please enter word");
char c = str.charAt(str.length()-1);
if(str.length() == 1) return Character.toString(c);
return c + reverseString(str.substring(0,str.length()-1));}}
View Replies
View Related