Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
paint(Graphics gr) returns nullpointerexception ?
achana chan
Ranch Hand
Posts: 277
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi.
I want to use the following codes to "paint" and image, and get a null pointer exception on my
Java
console.
Can someone please give me a hint what might have gone wrong ???
... import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.ImageIcon; import java.net.*; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class demoApplet extends Applet implements ActionListener { TextField EmpNo = new TextField(10); TextField EmpName = new TextField(20); Button search = new Button("Search"); // Image thisCliPic; int imageX, imageY; // for positioning the image Image backBuffer = createImage(400,200); Graphics backBufferG = backBuffer.getGraphics(); // ... tedious codes public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); String myDateFormat = "dd-MMM-yyyy"; // candidate for .properties file // Figure out which component caused the event if (obj == search) { // If no demo object was created, then exit // Otherwise clear display field and get empno entered by user if (demo == null) { return; } EmpName.setText(""); // int thisEmpNo = 0; try { System.out.println("=>demoApplet : EmpNo.getText()= " + EmpNo.getText()); thisEmpNo = Integer.parseInt(EmpNo.getText()); } //end of try-block catch (Exception ex) {ex.printStackTrace();} // // Get the emp record System.out.println("=>demoApplet : retrieving the database record. "); empRecord thisEmpRec = demo.query(thisEmpNo); // populate if (thisEmpRec != null) { EmpName.setText(thisEmpRec.ename); // // load and convert the image thisCliPic = new ImageIcon(thisEmpRec.picture).getImage(); // } // end of it-then-else } // end of if-then-else } //end of actionPerformed()-method // // ---------- something gone wrong here ..[B] public void paint(Graphics grph) { update(grph); } public void update(Graphics g) { int w = size().width; int h = size().height; if (backBuffer == null || backBuffer.getWidth(this) < w || backBuffer.getHeight(this) < h) { backBuffer = createImage(w, h); } backBufferG.clearRect(0, 0, w, h); backBufferG.drawImage(thisCliPic, imageX, imageY, this); g.drawImage(backBuffer,0,0,this); } [/B] // --------------- put into utilities bin // public static String toStringDate(Date inputDate, String myDateFormat) { String dteFmt = myDateFormat; SimpleDateFormat formattedDate = new SimpleDateFormat(dteFmt); return formattedDate.format(inputDate); } // } // end of demoApplet-applet
humanum errare est.
Dirk Schreckmann
Sheriff
Posts: 7023
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Did the error message include any information on what part of the program may have caused the NullPointerException (such as a line number)?
[
How To Ask Good Questions
] [
JavaRanch FAQ Wiki
] [
JavaRanch Radio
]
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
multiple painting components
Sunday 11pm - defeated by BLOB . Want to see whether you can do this ?
How to use AWT's Frame()???
Help : can I load a byte array as an image ???
Images on Applet?
More...