Drukowanie obiektu klasy string na drukarce.

0

Hi, napisałem prosty program, który w założeniu ma pobierać rekordy z bazy danych a następnie je drukować. Czy ktoś mógłby rzucić okiem na ten kod i ew. zwrócić uwagę co robię źle.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.sql.*;
import javax.print.attribute.*;
import javax.swing.*;
import java.util.List.*;
import java.util.Vector;

class Frame extends JFrame {
    public Frame() {
        setSize(160, 120);
        setTitle("");

        action();
    }

    public void action() {
        createUI();
    }

    public void createUI() {
        setLayout(new GridLayout(5, 2));
        add(new JLabel("Nazwa bazy:"));
        add(fNameBase);
        add(new JLabel("User:"));
        add(fNameUser);
        add(new JLabel("Password"));
        add(fPass);
        add(new JLabel("Nazwa tabeli:"));
        add(fNameTab);

        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    java.sql.Connection pol = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + fNameBase.getText(),
                                                                                                                                                   fNameUser.getText(), new String(fPass.getPassword()));
                    st = pol.createStatement();
                    get();
                } catch (Exception ek) {
                    JOptionPane.showMessageDialog(null, "Nie nazwiazno polaczenia");
                }
                
            }
            public void get() {
                            String polecenie = "select * from " + fNameBase.getText() + "." + fNameTab.getText();
                            try {
                                rs = st.executeQuery(polecenie);
                                while (rs.next()) {
                                    s1+="\n" + rs.getString(1);
                                    s2 +="\n" + rs.getString(2);
                                    s3 +="\n" + rs.getString(3);
                                    s4 += "\n" +rs.getString(4);
                                    panel.setStr(s1 +s2 +  s3 +  s4);
                                   panel = new PrintPanel();
                                   panel.setStr(s1 +s2 +  s3 +  s4);

                                   System.out.println(s1 + ", " + s2 + "," + s3 + "," + s4);
                                }
                                st.close();
                            } catch (Exception ek) {
                            }
                             try {
                                    PrinterJob pj = PrinterJob.getPrinterJob();
                                    panel = new PrintPanel();
                                    pj.setPrintable(panel);
                                    System.out.println(panel.getStr());
                                     pras = new HashPrintRequestAttributeSet();
                                    if (pj.printDialog(pras)) {
                                        pj.print(pras);
                                    }
                                } catch (Exception e) {
                                     System.out.println("linia 88:" + e.toString());
                                }
                        }
        });
        add(b);

    }
    JTextField fNameBase = new JTextField();
    JTextField fNameUser = new JTextField();
    JPasswordField fPass = new JPasswordField();
    JTextField fNameTab = new JTextField();
    java.sql.Statement st;
    JButton b = new JButton("Print");
    ResultSet rs;
    String s1, s2, s3, s4;
    PrintRequestAttributeSet pras;
    PrintPanel panel;

}

class PrintPanel implements Printable {
        public int print(Graphics g, PageFormat pageFormat, int page) {
            int i = 0;
            while(__str.get(i) != null) {
            g.drawString(__str.get(i), 10,y+=14);
            i++;
            }
            return (page == 0 ? PAGE_EXISTS : NO_SUCH_PAGE);
        }
        public void setStr(String arg) {
            __str.add(arg);
        }
        public String getStr() {
            String s = new String("");
            int i = 0;
            while(__str.get(i) != null) {
                s += "" + __str.get(i);
                i++;
            }
            return s;
        }
        private Vector<String> __str;
        int y = 10;
    }

public class Main {
    public static void main(String[] args) {

        Frame f = new Frame();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
0

Nieco inaczej - czy ktoś dostrzega co tutaj powoduje rzucenie wyjątku NullPointerException?

try {
                                    PrinterJob pj = PrinterJob.getPrinterJob();
                                    panel = new PrintPanel();
                                    pj.setPrintable(panel);
                                    System.out.println(panel.getStr());
                                     pras = new HashPrintRequestAttributeSet();
                                    if (pj.printDialog(pras)) {
                                        pj.print(pras);
                                    }
                                } catch (Exception e) {
                                     System.out.println("linia 88:" + e.toString());
                                }
0

Od czego masz stack trace'y?

0
[Ljava.lang.StackTraceElement;@6b97fd

Cóż z tego wynika?

0

Omg,

                   catch (Exception e) {
                                    e.printStackTrace();
                   }

1 użytkowników online, w tym zalogowanych: 0, gości: 1