小编sea*_*eal的帖子

为什么Kotlin收到这样的UndeclaredThrowableException而不是ParseException?

我有一个extension方法,在Kotlin中将字符串转换为Date.

fun String.convertToDate() : Date {
  var pattern: String = "dd-mm-yyyy"
  val dateFormatter = SimpleDateFormat(pattern)
  return dateFormatter.parse(this) // parse method throw ParseException
}
Run Code Online (Sandbox Code Playgroud)

这是我试图捕获可能的异常的代码.

    try {
        "22---2017".convertToDate()
    } catch (ex: ParseException) {
        // ParseException supposed to be caught in this block

        logger.error("Parse exception occur")      
    } catch (ex: Exception) {

        // ParseException caught in this block

        logger.error("Exception occur")            
    }
Run Code Online (Sandbox Code Playgroud)

ParseException过去块就是抓住了Exception被捕获.但它应该在ParseException块中捕获吗?我在这里错过了什么?

=== 更新 ===

我正在开发一个Spring MVC项目.我已经在简单的独立kotlin程序中运行代码,其中它的行为相应.但在我的春季项目中,它表现不同.我给出了完整的代码ControllerService图层.

调节器

@PostMapping
@PreAuthorize("hasAnyRole('USER','ROLE_USER','ROLE_ADMIN','ADMIN')")
fun postAttendance(@RequestBody attendanceJson: …
Run Code Online (Sandbox Code Playgroud)

java exception-handling try-catch kotlin

14
推荐指数
1
解决办法
2631
查看次数

如何在java中增加Map中特定键的值?

在C++中,如果我声明像std :: map m这样的地图

然后我可以用这种方式增加地图中特定键的值

m[key]++
Run Code Online (Sandbox Code Playgroud)

在Java中我声明了一个地图

Map<Integer, Integer> m = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

并以这种方式增加特定键的值:

m.put(key, m.get(key).intValue() + 1)
Run Code Online (Sandbox Code Playgroud)

我的问题:有没有捷径或更好的方法来做到这一点?

java map

11
推荐指数
3
解决办法
6404
查看次数

如何使用hibernate标准连接多个表,其中实体关系不是直接的?

我有三个实体.那些是:

@Entity
public class Organization {
    @Id
    private long id;
    @Column
    private String name;
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Book {
    @Id
    private Long id;
    @Column
    private String name;
    @ManyToOne
    private Organization organization;
}
Run Code Online (Sandbox Code Playgroud)
@Entity
public class Account  {
   @Id
   private Long id;
   @Column
   private String name;
   @ManyToOne
   private Book book;
}
Run Code Online (Sandbox Code Playgroud)

在这三个实体中,我想执行以下sql:

SELECT acc.name, acc.id
FROM account acc
JOIN book b on acc.book_id = b.id
JOIN organization org on b.organization_id = org.id
WHERE org.name = 'XYZ'
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Account实体与Organization …

java sql join hibernate-criteria

6
推荐指数
2
解决办法
4万
查看次数

java中文本区域的大小

我正在编写基本GUI的代码.我需要一个文本区域.但我无法使文本区域达到我想要的大小.我使用setPreferredSize方法来设置文本区域的维度.但它没有用.我也试过setSize方法但也没用.这是我的书面代码.

 private void textArea() {
    setTitle("TextArea");
    setSize(700, 500);
    setLayout(new BorderLayout());


    JTextArea textArea = new JTextArea();

    textArea.setPreferredSize(new Dimension(100,100));
    System.out.println(textArea.getSize());

    textArea.setBackground(Color.GREEN);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(false);


    add(textArea,BorderLayout.CENTER);
}
Run Code Online (Sandbox Code Playgroud)

对不起,我的英语不好.提前致谢.

java swing jcomponent jtextarea preferredsize

5
推荐指数
1
解决办法
2万
查看次数

得到“没有定义合格的 bean 类型。”

我遇到异常 -

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.muztaba.service.VerdictServiceImpl] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
at com.muztaba.service.App.task(App.java:35)
at com.muztaba.service.App.main(App.java:28)
Run Code Online (Sandbox Code Playgroud)

这是我得到异常的班级。

@Component
public class App {

QueueService<Submission> queue;

Compiler compiler;

VerdictService verdictService;

public static void main( String[] args ) {
    new App().task();
}

private void task() {
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    queue =  context.getBean(QueueImpl.class);
    compiler = context.getBean(CompilerImpl.class);
    verdictService = context.getBean(VerdictServiceImpl.class); //here the exception thrown. 

    while (true) {
        if (!queue.isEmpty()) {
            Submission submission = queue.get();
            compiler.submit(submission);
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

spring ioc-container autowired

5
推荐指数
1
解决办法
1万
查看次数

为什么不@NotNull anotation删除警告?

我写了一个实现类似接口的类.我使用@NotNull注释来抑制方法参数中的waring.但它仍然显示了warning.The IDE自动导入这个包com.sun.istack.internal.NotNull@NotNull.为什么这是在讨价还价?不使用此注释如何删除此警告?
我正在使用带有java 8 SE的Inteij Ultimate.这是我的代码片段.在此输入图像描述

谢谢.

java warnings annotations notnull

4
推荐指数
1
解决办法
2484
查看次数

如何在java 8流中进行条件操作以跳过下一个流操作?

在传统方式中,我已经编写了这个简单的java for循环

public String setString(String string) {  
StringBuilder stringBuilder = new StringBuilder();

    // This for loop I want to convert into stream. 
    for (char c : string.toCharArray()) {
       if (Character.isSpaceChar(c))
           stringBuilder.append(c);
       else if (isBangla(c))
           stringBuilder.append(get(c));
    }
    return stringBuilder.toString();
}

String get(int c) {
    return keyMap.getMap((char) c); // Map<Character, String> keyMap
}

boolean isBangla(int codePoint) {
    return ((codePoint >= BANGLA_CHAR_START && codePoint <= BANGLA_CHAR_END) &&
        (codePoint <= BANGLA_NUMBER_START || codePoint >= BANGLA_NUMBER_END)));
}
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情.

String str = string.chars()
            .filter(i -> Character.isSpaceChar(i)) …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

2
推荐指数
1
解决办法
2827
查看次数