Java异常nullpointerexception错误(Java Exception nullpointerexception error)
什么导致null异常? 我根本无法看到它..请第二组眼睛将帮助这个初学者java学生..这个应用程序基本上捕获有关游戏团队的5条信息并插入到mySQL数据库中。
错误如下所示:InsertTeamInfo.access $ 100中InsertTeamInfo.insertRecord(InsertTeamInfo.java:130)中的线程“AWT-EventQueue-0”java.lang.NullPointerException异常(InsertTeamInfo.java:11)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class InsertTeamInfo extends JFrame{ private String sql; private Connection con; private Statement stat; private JButton btnSave; private JTextField txtTeam; private JTextField txtCity; private JTextField txtYear; private JTextField txtLoserTeam; private JTextField txtLoserCity; private JLabel lblTeam; private JLabel lblCity; private JLabel lblYear; private JLabel lblLoserTeam; private JLabel lblLoserCity; public InsertTeamInfo(){ super("Favorite Team"); btnSave = new JButton("Save"); txtTeam = new JTextField(""); txtCity = new JTextField(""); txtYear = new JTextField(""); txtLoserTeam = new JTextField(""); txtLoserCity = new JTextField(""); lblTeam = new JLabel("Team"); lblCity = new JLabel("City"); lblYear = new JLabel("Year Played"); lblLoserTeam = new JLabel("Loser Team"); lblLoserCity = new JLabel("Loser City"); txtYear.setEditable(true); txtLoserTeam.setEditable(true); txtLoserCity.setEditable(true); txtTeam.setEditable(true); txtCity.setEditable(true); } public void launchJFrame(){ //width - height setSize(500, 300); getContentPane().setLayout(null); getContentPane().setBackground(Color.white); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); getContentPane().add(btnSave); getContentPane().add(txtTeam); getContentPane().add(txtCity); getContentPane().add(txtYear); getContentPane().add(txtLoserTeam); getContentPane().add(txtLoserCity); getContentPane().add(lblTeam); getContentPane().add(lblCity); getContentPane().add(lblYear); getContentPane().add(lblLoserTeam); getContentPane().add(lblLoserCity); lblTeam.setBounds(new Rectangle(65,20,100,30)); lblCity.setBounds(new Rectangle(65,55,100,30)); lblYear.setBounds(new Rectangle(65, 85, 100, 30)); lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30)); lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30)); txtTeam.setBounds(new Rectangle(210, 20, 150, 30)); txtCity.setBounds(new Rectangle(210, 54, 150, 30)); txtYear.setBounds(new Rectangle(210, 86, 150, 30)); txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30)); txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30)); btnSave.setBounds(new Rectangle(65, 190, 90, 30)); setVisible(true); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { shutDown(); } }); btnSave.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Handle Save button click event insertRecord(); } }); } private void insertRecord(){ try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231"); } catch(Exception ex){ JOptionPane.showMessageDialog( null, "Error connection to database."); System.exit(0); } try { sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = con.prepareStatement(sql); preparedStatement.setString(1, txtTeam.getText()); preparedStatement.setString(2, txtCity.getText()); preparedStatement.setString(3, txtYear.getText()); preparedStatement.setString(4, txtLoserTeam.getText()); preparedStatement.setString(5, txtLoserCity.getText()); preparedStatement.executeUpdate(); JOptionPane.showMessageDialog( null, "Data Inserted Successfully"); } catch(SQLException ex){ ex.printStackTrace(); JOptionPane.showMessageDialog( null, "Data Insert failed."); } try{ stat.close(); con.close(); } catch(SQLException ex){ JOptionPane.showMessageDialog( null, "All active connection closed to the database."); } } private void shutDown(){ int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?"); if(returnVal == JOptionPane.YES_OPTION){ System.exit(0); } } public static void main(String[] args){ InsertTeamInfo gui = new InsertTeamInfo(); gui.launchJFrame(); } }
What is causing the null exception here? I am simply unable to see it.. please second set of eyes would help this beginner java student..This application basically captures 5 pieces of info about a game team and insert into mySQL database.
The error is as below: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at InsertTeamInfo.insertRecord(InsertTeamInfo.java:130) at InsertTeamInfo.access$100(InsertTeamInfo.java:11)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class InsertTeamInfo extends JFrame{ private String sql; private Connection con; private Statement stat; private JButton btnSave; private JTextField txtTeam; private JTextField txtCity; private JTextField txtYear; private JTextField txtLoserTeam; private JTextField txtLoserCity; private JLabel lblTeam; private JLabel lblCity; private JLabel lblYear; private JLabel lblLoserTeam; private JLabel lblLoserCity; public InsertTeamInfo(){ super("Favorite Team"); btnSave = new JButton("Save"); txtTeam = new JTextField(""); txtCity = new JTextField(""); txtYear = new JTextField(""); txtLoserTeam = new JTextField(""); txtLoserCity = new JTextField(""); lblTeam = new JLabel("Team"); lblCity = new JLabel("City"); lblYear = new JLabel("Year Played"); lblLoserTeam = new JLabel("Loser Team"); lblLoserCity = new JLabel("Loser City"); txtYear.setEditable(true); txtLoserTeam.setEditable(true); txtLoserCity.setEditable(true); txtTeam.setEditable(true); txtCity.setEditable(true); } public void launchJFrame(){ //width - height setSize(500, 300); getContentPane().setLayout(null); getContentPane().setBackground(Color.white); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); getContentPane().add(btnSave); getContentPane().add(txtTeam); getContentPane().add(txtCity); getContentPane().add(txtYear); getContentPane().add(txtLoserTeam); getContentPane().add(txtLoserCity); getContentPane().add(lblTeam); getContentPane().add(lblCity); getContentPane().add(lblYear); getContentPane().add(lblLoserTeam); getContentPane().add(lblLoserCity); lblTeam.setBounds(new Rectangle(65,20,100,30)); lblCity.setBounds(new Rectangle(65,55,100,30)); lblYear.setBounds(new Rectangle(65, 85, 100, 30)); lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30)); lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30)); txtTeam.setBounds(new Rectangle(210, 20, 150, 30)); txtCity.setBounds(new Rectangle(210, 54, 150, 30)); txtYear.setBounds(new Rectangle(210, 86, 150, 30)); txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30)); txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30)); btnSave.setBounds(new Rectangle(65, 190, 90, 30)); setVisible(true); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { shutDown(); } }); btnSave.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Handle Save button click event insertRecord(); } }); } private void insertRecord(){ try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231"); } catch(Exception ex){ JOptionPane.showMessageDialog( null, "Error connection to database."); System.exit(0); } try { sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = con.prepareStatement(sql); preparedStatement.setString(1, txtTeam.getText()); preparedStatement.setString(2, txtCity.getText()); preparedStatement.setString(3, txtYear.getText()); preparedStatement.setString(4, txtLoserTeam.getText()); preparedStatement.setString(5, txtLoserCity.getText()); preparedStatement.executeUpdate(); JOptionPane.showMessageDialog( null, "Data Inserted Successfully"); } catch(SQLException ex){ ex.printStackTrace(); JOptionPane.showMessageDialog( null, "Data Insert failed."); } try{ stat.close(); con.close(); } catch(SQLException ex){ JOptionPane.showMessageDialog( null, "All active connection closed to the database."); } } private void shutDown(){ int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?"); if(returnVal == JOptionPane.YES_OPTION){ System.exit(0); } } public static void main(String[] args){ InsertTeamInfo gui = new InsertTeamInfo(); gui.launchJFrame(); } }
原文:
最满意答案
使用
#links a {clear:left;float:left}
float
将允许链接的大小,clear
将阻止链接在同一行上。您可能需要在
#links
容器中添加一个clear:left
,具体取决于您的设计。编辑
你提问的一个小教程:
有两种类型的元素,内联和块。 内联线显示没有中断。 块元素占据了整条线并转移到下一个元素。
内联元素不能具有其宽度或高度样式。 块可以。
<a>
是内联元素。 通过将其显示设置为阻止,您可以告诉它每次都创建一个新行。
float
给出了元素内联行为,因此它们会在彼此旁边碰撞并流到下一行。float
还允许您设置元素的宽度/高度。 这是两者之间的混合。clear属性停止内联浮动并返回到正常的块行为(每次都有新行)。
您不需要
display:block
和float:
在同一时间。另一种解决方案将涉及
display:inline-block
,但这在几个浏览器中不受支持,因此不受鼓励(尽管我觉得它非常方便)。Use
#links a {clear:left;float:left}
The
float
will allow the link to be sized, and theclear
will prevent the links from being on the same line.You may need to add a
clear:left
to the#links
container depending on your design.EDIT
A little tutorial since you asked:
There are two types of elements, inline and block. Inline ones show in a line with no breaks. Block elements take up the whole line and move to the next one.
Inline elements can't have their width or height styled. Blocks can.
<a>
is an inline element. By setting its display to block, you tell it to make a new line every time.
float
gives elements inline behavior so they bump up next to eachother and flow over onto the next line.float
also allows you to style the width/height of the element. It's sort of a mix between the two.The clear attribute stops the inline floating and goes back to normal block behavior (new lines every time).
You won't need
display:block
andfloat:
at the same time.Another solution would involve
display:inline-block
, but this is not supported in several browsers so isn't encouraged (although I find it pretty handy).
相关问答
更多-
继承自Canvas(Inheriting from Canvas)[2022-07-31]
仅供参考 - 以下是我修复它的方法: 我不得不删除代码隐藏中的所有引用到“mainCanvas” 然后我重新编译并得到一个新的Xaml错误:在行中告诉我,我必须用“x:Name”替换“Name”。 FYI - Here's how I fixed it: I had to remove ... -
添加android:textColorLink="yourcolorhere"到您的TextView Add android:textColorLink="yourcolorhere" to your TextView
-
你错过了.includebox上的l 编辑2 这是我简化的相同代码 ,我会这样做。 删除.margins div,不必要 重构你的CSS,这样你在第二个盒子中的样式所有div都是相同的,并且唯一的区别是一个漂浮在右边 将CSS规则传播到单独的行上,如果它具有单个属性,则仅将规则设为一行 移除position:relative和top和right position:relative - 再次,不需要 我希望有帮助:) 编辑你也不需要任何接近你所拥有的样式,这些都可以,因为他们什么都不做(除非你有他们出于我在你 ...
-
尝试这个: $("#content_wrapper").css('cursor', 'default').click(function(e) { e.stopPropagation(); }); 在这里演示 Try this: $("#content_wrapper").css('cursor', 'default').click(function(e) { e.stopPropagation(); }); DEMO HERE
-
LinkButton或HyperLink?(LinkButton or HyperLink?)[2022-06-03]
我会说一个HyperLink命中特定的URL来存储必要的会话数据,然后在存储信息后使用Response.Redirect重定向到下一页。 HyperLink的URL将指向您的服务器,以便您可以存储信息,然后在存储必要的数据之后使用重定向将用户指向正确的端点。 例 HyperLink的URL指向~/yourpage.aspx?state=NY ,其中target="_blank" 服务器响应URL并检查查询字符串。 如果查询字符串存在,则存储数据( if (Request.QueryString["state ... -
View all使用align-items: flex-start; contentContainer div上的contentContainer .contentContainer { display: flex; flex-direction: column; margin-bottom: 15px; align-items: flex-start; } .text { border: 1px solid black; }
继承CSS宽度(Inheriting CSS Width)[2023-12-03]
替换width: auto; 在100% fieldset 。 fieldset { background:#ebeced; border: none; border-top: 1px solid #d0d2d5; border-radius: 0 0 4px 4px; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.75); margin: 5px 0 -20px -20px; padding: 18px 2 ...CSS继承宽度不起作用(CSS Inheriting Width not working)[2022-02-03]
更改位置:绝对位置:div.job类的相对位置 Change position:absolute to position:relative for div.job class使用 #links a {clear:left;float:left} float将允许链接的大小, clear将阻止链接在同一行上。 您可能需要在#links容器中添加一个clear:left ,具体取决于您的设计。 编辑 你提问的一个小教程: 有两种类型的元素,内联和块。 内联线显示没有中断。 块元素占据了整条线并转移到下一个元素。 内联元素不能具有其宽度或高度样式。 块可以。 是内联元素。 通过将其显示设置为阻止,您可以告诉它每次都创建一个新行。 float给出了元素内联行为,因此它们会在彼此旁 ...相关文章
更多- Java 异常处理
- java Exception异常处理实例
- EXT4 Store Batch Sync Exception
- Hive 终端产生的问题 (Failed to start database 'metastore_db', see the next exception for details.)
- [Hadoop Err] Call to ? failed on local exception: java.net.NoRouteToHostException: No route to host
- Struts2类型转换器出了一个NullPointerException,那位大哥指点一下
- EXT4 Store Load For Gird Exception
- java异常处理分类
- [Hadoop] Error: JAVA_HOME is not set
- Hadoop 异常记录 ERROR: org.apache.hadoop.hbase.MasterNotRunningException: Retried 7 times
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)