Elektroda.pl
Elektroda.pl
X
Please add exception to AdBlock for elektroda.pl.
If you watch the ads, you support portal and users.

JAVA (JDBC) java.lang.NullPointerException HELP!!!

bebop 21 Aug 2006 13:42 10977 3
Eltrox Hurton
  • #1
    bebop
    Level 11  
    Witam jestem poczatkujacy, napisalem prosty program do komunikacji z baza danych (postgres) i mam problem z funkcja - ResultSetMetaData.getColumnName() bo nie wiem dlaczego zamiast zwocic tablice Stringow wywala wyjatek java.lang.NullPointerException
    Kod:
    
    import java.sql.*;
    
    public class ColumnName {
    	Connection connection;        
    	Statement statement;       
    	DatabaseMetaData dbmd;
    	String[] columnNames;
    	public ColumnName()
        throws ClassNotFoundException, SQLException{
    		String database = ".";
    		String username = ".";
    		String password = ".";
    		Class.forName("org.postgresql.Driver"); //load the driver
    		connection = DriverManager.getConnection("jdbc:postgresql:"+database,
                                         username,
                                         password); //connect to the db
    		dbmd = connection.getMetaData(); //get MetaData to confirm connection
    		System.out.println("Connected to "+dbmd.getDatabaseProductName()+" "+
                           dbmd.getDatabaseProductVersion()+" successful.\n");
    		statement = connection.createStatement(); //create a statement that we can use later
     
    		int columnCount;
    		String query = "select * from products";
    		ResultSet rs = statement.executeQuery(query);
    		ResultSetMetaData rsmd = rs.getMetaData();
    		columnCount=rsmd.getColumnCount();
    		System.out.println("Liczba kolumn "+columnCount);
    		for(int i=1; i<columnCount+1;i++){
    			if(rsmd.getColumnName(i)!=null){
    				System.out.println(i);
    				columnNames[i-1]=rsmd.getColumnName(i);
    			}
    			else System.out.println("null");
    		}
    		System.out.println("---------------------");
    		System.out.println(columnNames);
    		System.out.println("---------------------");
    		rs.close();
    	}
    	public static void main (String args[]) {
    	    try{
    	    	ColumnName columnName = new ColumnName();
    	    }catch (Exception ex){
    	    	System.out.println("***Exception:\n"+ex);
    	    	ex.printStackTrace();
    	    }
    	}
    }
    

    Baza skalada sie z jednej tabeli :
    
    create table products (
    id integer primary key,
    names varchar(32),
    price decimal(10,2),
    quan integer
    );

    i wypelniana jest wartosciami:
    
    delete from products;
    insert into products values(1, 'Kabel UTP 5', 1.00, 3);
    insert into products values(2, 'Karta Siecieowa 10/100 M', 15.00, 4);
    insert into products values(3, 'Router Edimax 6104K', 105.00, 6);
    insert into products values(4, 'ASUS CD-S520', 55.00, 1);
    insert into products values(5, 'LG GCR-8525 52xSpeed OEM', 55.00, 6);
    insert into products values(6, 'Athlon 64 3000 Socket 939', 369.00,7);
    

    Jezeli ktos wie dlaczego tak sie dziaje to prosze o pomoc .
  • Eltrox Hurton
  • Helpful post
    #2
    JanuszPulit
    Level 17  
    Ten wyjątek nie jest spowodowany metodą tylko tym że nie utworzyłeś tablicy a próbujesz wstawiać do niej wartości.

    Jak już pobierzesz ilość kolumn to musisz utworzyć tablice

    
    
    columnNames = new String[columnCount];
    
  • Eltrox Hurton
  • #3
    bebop
    Level 11  
    wielkie dzieki nie zwrocilem na to uwagi :)
  • #4
    cogliostro666
    Level 11  
    Witam

    Mam podobny problem, ale dużo mniej wiedzy o programowaniu. Kod wygląda następująco:

    import java.io.*; 
    import java.util.*; 
    import java.awt.*; 
    import java.awt.image.*; 
    import javax.imageio.*; 
    import javax.media.*; 
    import javax.media.control.*; 
    import javax.media.format.*; 
    import javax.media.util.*; 
    import icommand.vision.MotionDetectionEffect; 
     
     
    public class camHandler{ 
    private CaptureDeviceInfo deviceInfo; 
    public Player player; 
     
    public camHandler() {} 
     
    public void ini() { 
    // Create capture device 
        Vector deviceList = CaptureDeviceManager.getDeviceList(new VideoFormat(VideoFormat.RGB)); 
        deviceInfo = (CaptureDeviceInfo)deviceList.firstElement(); 
        /* 
         * 2. Tworzymy procesor dla pierwszej dostepnej kamery 
         */ 
        try{ 
        player = Manager.createRealizedPlayer(deviceInfo.getLocator()); 
    this.deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0"); 
    player = null; 
    try { 
    player = Manager.createRealizedPlayer(deviceInfo.getLocator()); 
    } 
    catch (CannotRealizeException ex) { 
    ex.printStackTrace(); 
    } 
    catch (NoPlayerException ex) { 
    ex.printStackTrace(); 
    } 
    catch (IOException ex) { 
    ex.printStackTrace(); 
    } 
    player.start(); 
        } catch (NoPlayerException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            System.out.println ("No player"); 
        } catch (CannotRealizeException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            System.out.println ("Cannot realize exception"); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }finally  
        { 
            System.out.println ("Finally!"); 
        }  // end finally 
    // Wait a few seconds for camera to initialise (otherwise img==null) 
    try { 
    Thread.sleep(2500); 
    } 
    catch (InterruptedException ex1) { 
    ex1.printStackTrace(); 
    } 
    } //init 
     
     
     
    public BufferedImage capture(String path) { 
     
    // Grab a frame from the image device 
     
     
    // Convert frame to an buffered image so it can be processed and saved 
    Image img = (new BufferToImage( (VideoFormat) bufor.getFormat()).createImage( 
    bufor)); 
     
    BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), 
    BufferedImage.TYPE_INT_RGB); 
    Graphics2D g = buffImg.createGraphics(); 
    g.drawImage(img, null, null); 
     
    // Overlay curent time on image 
     
    // Save image to disk as PNG 
    try { 
    // ImageIO.write(buffImg, "png", new File(path)); 
    System.out.println("images is saved " + path + new Date().toString()); 
    } 
    catch (Exception ex2) { 
    } 
     
    return buffImg; 
     
    } 
     
    public void closeConn() { 
    // Stop using webcam 
    player.close(); 
    player.deallocate(); 
    System.exit(0); 
     
    } 
     
    public boolean motion (Buffer buff, Buffer bufff){ 
         
        bufor=buff; 
        try { 
            Thread.sleep(3000); 
        } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
        bufor=bufff; 
         
        MotionDetectionEffect ruch = new MotionDetectionEffect(); 
        if(ruch.process(buff, bufff)==0){ 
        return true; 
        } 
        else{ 
            return false; 
        } 
     
    } 
     
        //Buffer x; 
        // Grab a frame from the image device 
        FrameGrabbingControl frameGrabber = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); 
        public Buffer bufor = frameGrabber.grabFrame(); 
         
        //return x; 
         
     
     
     
    public static void main(String[] args) throws Exception { 
    BufferedImage current; 
     
    camHandler test = new camHandler(); 
     
    test.ini(); 
     
    Buffer pierwszy = null; 
    Buffer drugi = null; 
     
    current = test.capture("c:\\webcam.png"); 
    for (int i = 0; i < 10; i++) { 
    BufferedImage next = test.capture("c:\\webcam" + i + ".png"); 
    Thread.sleep(3000); 
    if (test.motion(pierwszy, drugi)==true) { // if motion is detected 
     
    System.out.println("Motion"); 
    ImageIO.write(next, "png", new File("c:\\next" + i + ".png")); 
    ImageIO.write(next, "png", new File("c:\\current" + i + ".png")); 
     
    } 
    current = next; 
    } 
    test.closeConn(); 
     
    } 
    }


    W założeniu program ma wykrywać ruch i to będzie wyzwalaczem zapisywania klatek wideo. Docelowo klatki będą zapisywane jako animowany GIF ale z tym powinienem sobie poradzić (proszę, nie pytajcie dlaczego GIF). Eclipse wypluwa mi następujący bład:
    Exception in thread "main" java.lang.NullPointerException 
        at camHandler.<init>(camHandler.java:135) 
        at camHandler.main(camHandler.java:146) 


    co przenosi mnie do linijek:
    FrameGrabbingControl frameGrabber = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");

    i
    camHandler test = new camHandler();

    Jeżeli ktoś mógłby mi podać jakieś łopatologiczne rozwiąznie, byłbym wdzięczny.