继续得到此异常:java.lang.IllegalArgumentException:Width(0)和height(0)不能是< = 0

时间:2014-03-02 17:36:49

标签: java image

我想显示一个图标,这就是我创建方法的原因,但是当我调用该方法时,图像不希望出现。我试图添加一个按钮来检查方法是否有效。我不知道该方法是否正常工作。该方法应该有效,但问题是图像不会出现。

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.sql.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;`


public final class Login extends javax.swing.JFrame {
Connection conn=null;
ResultSet rs=null;
PreparedStatement ps=null;


public Login() {
      image3();
      initComponents();
      conn = javaconnect.ConnectDB();
      DateNow();
      txt_username.requestFocus();
      }

public final void image3(){
try{
     String sql= "SELECT * from Picture";   
     ps = conn.prepareStatement(sql);
     rs=ps.executeQuery();
     if (rs.next()){

     byte[] imagedata = rs.getBytes("Background");
     format = new ImageIcon(imagedata);

     Image img = format.getImage();
     ImageIcon img2 = new    ImageIcon(ScaledImage(img,showimage.getWidth(),showimage.getHeight()));
     showimage.setIcon(img2);


      rs.close();
      ps.close();      
     }
 }
        catch(Exception e){}

 finally {

        try{
           rs.close();
            ps.close();
            //conn close
            }

        catch(Exception e){}

     }
    }

public Image ScaledImage(Image img, int w, int h){
BufferedImage resizedImage = new BufferedImage (w,h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0,0,w,h,null);
g2.dispose();


return resizedImage;
}

异常

java.lang.NullPointerException
at Login.image3(Login.java:43)
at Login.<init>(Login.java:19)
at Login$2.run(Login.java:327)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

更改图像后获取新的异常();以下

conn=javaconnect.ConnectDB()

java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

2 个答案:

答案 0 :(得分:0)

如果您创建一个名为Machine

的新类
class Machine {
private String name;
private int code;

public Machine() {
    this("Arnie", 0);

    System.out.println("Constructor running!");
}

public Machine(String name) {
    this(name, 0);

    System.out.println("Second constructor running");

}

public Machine(String name, int code) {

    System.out.println("Third constructor running");
    this.name = name;
    this.code = code;
}

}

并在主类App

中使用它
public class App {
public static void main(String[] args) {
    Machine machine1 = new Machine();

    Machine machine2 = new Machine("Bertie");

    Machine machine3 = new Machine("Chalky", 7);
}

}

因此构造函数具有类的名称,您可以说它是一种在您使用该类的任何对象时自动运行的特殊方法。

在第一个构造函数中我使用了两个参数String和Integer,当你调用一个对象并将这些参数传递给它时,会自动运行那些带有这些参数的构造函数。

知道了吗?

答案 1 :(得分:-1)

我认为当你用你想要的参数调用它时,你需要让你的类的构造函数自动启动...我可能不明白