我有几个类和一个主要方法。该程序用于连接到 Access 数据库并检索信息。
JTextArea
我有一个类在一个框中仅处理 GUI(以显示结果) 。另一个类运行一个 while
循环并从数据库中提取数据并将其分配给一个字符串,如下所示:
line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
基本上我的问题是我如何String line
从 GUI 类访问,以便我可以txtArea.setText
用来显示line
记住 GUI 没有 Main 方法?
编辑:
为了解决这个问题,我创建了一个LineObject
作为line
参数的。然后我调用getLine
from ,void addComponents
但它给出了 nullPointerException?
这是 searchProps 类:
import java.awt.Container;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class searchProps
{
protected String price, area, query, suburb, date, SaleOrRent, strQuery, out, line="";
protected int agentID, upid, StreetNum, numBeds, numBaths, numGarages, ownerID, size;
protected boolean spool;
PropertyObject PropertyArray[] = new PropertyObject[3];
LineObject obj;
JFrame jf;
JTextArea txtArea = new JTextArea();
{
initialFrame();
addComponents();
}
public searchProps(int propID) //search using UPID only
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
Statement s = conn.createStatement();
query = ("SELECT * FROM Properties WHERE UPID = "+propID);
// Fetch table
s.execute(query);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
upid=rs.getInt(1);
StreetNum=rs.getInt(2);
suburb=rs.getString(3);
area=rs.getString(4);
price=rs.getString(5);
agentID= rs.getInt(6);
numBeds=rs.getInt(7);
numBaths=rs.getInt(8);
spool=rs.getBoolean(9);
numGarages=rs.getInt(10);
date=rs.getString(11);
ownerID=rs.getInt(12);
SaleOrRent=rs.getString(13);
size++;
line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
obj= new LineObject(line);
System.out.println(line);
String out = obj.getLine();
System.out.println(out);
}
// close and cleanup
s.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
void initialFrame()
{
jf=new JFrame();
jf.setSize (1300,700);
jf.setTitle("Property Oracle | Results Page");
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void addComponents()
{
Container con = jf.getContentPane();
con.setLayout(null);
txtArea.setBounds(20,30,1200,600);
con.add(txtArea);
txtArea.setText("UPID\tStreetNum\tSuburb\tArea\tPrice\tAgentID\tBedrooms\tBathrooms\tSwimming Pool\tGarages\tDate\tOwner\tSale/Rent\n");
out = obj.getLine();
System.out.println(out);
}
}
这是 LineObject 类: public class LineObject
{
protected String line;
public LineObject(String a)
{
line = a;
}
public String getLine()
{
return line;
}
}