<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5858392062262821415</id><updated>2024-10-07T01:49:16.708-03:00</updated><category term="java"/><category term="c"/><category term="certificação"/><category term="exception"/><category term="exceções"/><category term="interface"/><category term="jni"/><category term="native"/><category term="nativo"/><category term="nullpointerexception"/><category term="polimorfismo"/><category term="scjp"/><category term="tutorial"/><title type='text'>jotaGeek</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://jotageek.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://jotageek.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Hiram</name><uri>http://www.blogger.com/profile/07914627338457007583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLcSwtPgc15-EzkwZ73dp93N-5V8N0wnTDmFQ7-lx_0PWMrxqm-77AyNpYbHF4PaLQ7jQB1v_jesQmhs4aoyIfFxcnreihObYnIKJNc3wqBT3d8wCZ7ZPeb3mM_2L0jw/s220/cigarrinhosxn1.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5858392062262821415.post-3317974209974209694</id><published>2010-02-16T20:47:00.013-02:00</published><updated>2010-02-17T17:11:33.974-02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="interface"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="jni"/><category scheme="http://www.blogger.com/atom/ns#" term="native"/><category scheme="http://www.blogger.com/atom/ns#" term="nativo"/><category scheme="http://www.blogger.com/atom/ns#" term="tutorial"/><title type='text'>Tutorial simples de JNI no Linux c/ GCC</title><content type='html'>&lt;span xmlns=&quot;&quot;&gt;&lt;p&gt;&lt;strong&gt;Introdução&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;O JNI (Java Native Interface) é uma interface que viabiliza interagir a JVM com bibliotecas nativas do SO subjacente. É utilizada principalmente para tarefas de mais baixo nível que a API Java não oferece ou, ainda, quando necessita-se de uma performance maior sem o &quot;overhead&quot; da JVM no caminho.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;É importante ressaltar que a utilização de métodos nativos torna sua aplicação dependente de plataforma. Em Linux trabalharemos com arquivos de extensão SO (Shared Objects), já em Windows trabalha-se com DLLs (Dynamic Linked Libraries). O JNI, no entanto, não se limita a somente Linux e Windows, é possível utilizá-lo em outras plataformas (Solaris, AIX, etc) de acordo com a implementação JVM.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Esse tutorial tem o objetivo de mostrar um simples exemplo de utilização do JNI. Diga-se de passagem, a documentação da Sun sobre o assunto é muito rica e os links se encontram nas referências.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Primeiro passo:&lt;/strong&gt; criar a classe que conterá o(s) método(s) nativo(s).&lt;br /&gt;&lt;br /&gt;Crie um diretório &quot;Carnival&quot; (apenas por questão de organização), uma classe e um método com o modificador &quot;native&quot; sem implementação para trabalharmos no exemplo.&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%&quot;&gt;&lt;code&gt;public class Carnival&lt;br /&gt;{&lt;br /&gt;&lt;/p&gt;&lt;p&gt;    // Carregamento da biblioteca que será criada.&lt;br /&gt;   static {&lt;br /&gt;       System.loadLibrary(&quot;Carnival&quot;);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;/p&gt;&lt;p&gt;    // Método marcado com modificador &quot;native&quot;.&lt;br /&gt;   public native void dance(String verse);&lt;br /&gt;&lt;br /&gt;   public static void main(String[] args) {&lt;br /&gt;       new Carnival().dance(&quot;...pode chorar, mas chora...&quot;);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Para que a JVM possa &quot;linkar&quot; a chamada do método da classe Carnival com uma biblioteca compartilhada nativa, ela precisará saber o nome. Aqui carregamos a biblioteca assim que a classe Carnival é carregada pela JVM:&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%&quot;&gt;&lt;code&gt;static {&lt;br /&gt;   System.loadLibrary(&quot;Carnival&quot;);&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Logo em seguida declaramos o método &quot;dance()&quot; que será chamado pelo método &quot;main()&quot;. Como a implementação de um método &quot;native&quot; será escrita em outra linguagem (C ou C++), não iremos fornecer implementação e terminaremos a declaração com ponto-e-vírgula.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Segundo passo:&lt;/strong&gt; compilar a classe.&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;&lt;br /&gt;javac Carnival.java&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Ela será compilada normalmente, mesmo que as bibliotecas nativas ainda não estejam acessíveis. Você só não conseguirá executar ainda.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Terceiro passo:&lt;/strong&gt; utilizar o &quot;javah&quot; para gerar os headers em C/C++.&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;&lt;br /&gt;javah -jni Carnival&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;* Certifique-se que a classe esteja em seu &quot;classpath&quot;.&lt;br /&gt;&lt;br /&gt;Isso gerará um arquivo Carnival.h que será usado ao criar a implementação do método.&lt;br /&gt;&lt;br /&gt;Se quiser, examine o Carnival.h. Você encontrará os protótipos das funções que implementará em seguida.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Quarto passo: &lt;/strong&gt;implementar o método nativo.&lt;br /&gt;&lt;br /&gt;Crie um arquivo chamado Carnival.c e dentro dele virá a implementação do método &quot;dance()&quot;.&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%&quot;&gt;&lt;code&gt;#include &amp;lt;jni.h&amp;gt;&lt;br /&gt;#include &amp;quot;Carnival.h&amp;quot;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;JNIEXPORT void JNICALL&lt;br /&gt;Java_Carnival_dance(JNIEnv * env, jobject obj, jstring verse)&lt;br /&gt;{&lt;br /&gt;    const char * vstr = (*env)-&amp;gt;GetStringUTFChars(env, verse, NULL);&lt;br /&gt;    printf(&amp;quot;%s\n&amp;quot;, vstr);&lt;br /&gt;    return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Quinto passo:&lt;/strong&gt; compilar a implementação.&lt;br /&gt;&lt;br /&gt;Neste tutorial utilizaremos o GCC. No meu desktop utilizei um Makefile para poupar o trabalho, mas aqui vai o comando direto para compilar.&lt;br /&gt;&lt;br /&gt;Antes criaremos um diretório &quot;lib&quot; que irá conter o arquivo &quot;Shared Object&quot; gerado pelo GCC:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;mkdir lib&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;E então compilaremos:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;gcc -o lib/libCarnival.so -shared -Wl,-soname,libCarnival.so \&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;-I/opt/jdk/include \&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;-I/opt/jdk/include/linux Carnival.c \&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;-static -lc&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;É importante lembrar que a biblioteca precisa estar no PATH de bibliotecas do Linux, então defina a variável de ambiente LD_LIBRARY_PATH para o diretório &quot;lib&quot; criado:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;LD_LIBRARY_PATH=&quot;$(pwd)/lib&quot;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;export LD_LIBRARY_PATH&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Com isso, ao executar a classe, a &quot;JVM&quot; conseguirá encontrar a biblioteca libCarnival.so.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Sexto (e último) passo:&lt;/strong&gt; executar.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;java Carnival&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Espera-se, aqui, que seja impressa a mensagem que foi enviada através do parâmetro &quot;verse&quot; (String) pelo método &quot;dance()&quot; da classe Java Carnival.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Referências:&lt;/strong&gt;&lt;br /&gt;   &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://java.sun.com/docs/books/jni/html/start.html&quot;&gt;http://java.sun.com/docs/books/jni/html/start.html#26346&lt;/a&gt;&lt;br /&gt;   &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html&quot;&gt;http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html&lt;/a&gt;&lt;br /&gt;   &lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://www.ibm.com/developerworks/aix/library/au-JNI_AIX_PAPER.html&quot;&gt;http://www.ibm.com/developerworks/aix/library/au-JNI_AIX_PAPER.html&lt;/a&gt;&lt;/p&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://jotageek.blogspot.com/feeds/3317974209974209694/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://jotageek.blogspot.com/2010/02/tutorial-simples-de-jni-no-linux-c-gcc.html#comment-form' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/3317974209974209694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/3317974209974209694'/><link rel='alternate' type='text/html' href='http://jotageek.blogspot.com/2010/02/tutorial-simples-de-jni-no-linux-c-gcc.html' title='Tutorial simples de JNI no Linux c/ GCC'/><author><name>Hiram</name><uri>http://www.blogger.com/profile/07914627338457007583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLcSwtPgc15-EzkwZ73dp93N-5V8N0wnTDmFQ7-lx_0PWMrxqm-77AyNpYbHF4PaLQ7jQB1v_jesQmhs4aoyIfFxcnreihObYnIKJNc3wqBT3d8wCZ7ZPeb3mM_2L0jw/s220/cigarrinhosxn1.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5858392062262821415.post-3277222695655845802</id><published>2010-02-16T18:46:00.006-02:00</published><updated>2010-02-16T22:14:12.540-02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="certificação"/><category scheme="http://www.blogger.com/atom/ns#" term="exceções"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="polimorfismo"/><category scheme="http://www.blogger.com/atom/ns#" term="scjp"/><title type='text'>Quando as chamadas polimórficas mudam tudo...</title><content type='html'>Estudando p/ o exame &lt;a href=&quot;http://www.sun.com/training/certification/java/scjp.xml&quot;&gt;SCJP&lt;/a&gt;, ao fazer uma revisão de assuntos dos primeiros capítulos antes de prosseguir, notei uma coisa que não foi abordada pelo &lt;a href=&quot;http://www.submarino.com.br/produto/1/21498837/certificacao+sun+para+programador+java+6+guia+de+estudo&quot;&gt;livro&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Aparentemente, fazer a chamada polimórfica de um método declarado na interface geraria o mesmo resultado:&lt;br /&gt;&lt;blockquote&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;interface Doable {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public void doSomething();&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;}&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;class Okay implements Doable {&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;/span&gt;&lt;br /&gt;@Override&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public void doSomething() {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        System.out.println(&quot;Oi, sou o método doSomething subscrito em Okay.&quot;);&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    }&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public static void main(String[] args) {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        Okay o = new Okay();&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        Doable d = o;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        // Aparentemente o comportamento é o mesmo.&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        &lt;span style=&quot;font-weight: bold; color: rgb(51, 51, 255);&quot;&gt;d.doSomething(); // OK&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: rgb(51, 51, 255);font-family:courier new;font-size:100%;&quot;  &gt;        o.doSomething(); // OK&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    }&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;}&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;Será, então, impresso:&lt;br /&gt;&lt;blockquote&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Oi,  sou o método doSomething subscrito em Okay.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Oi, sou o método doSomething subscrito em Okay.&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;Ora, basta declarar o lançamento de exceções na declaração do método &quot;doSomething()&quot; da interface que tudo muda. Veja:&lt;br /&gt;&lt;blockquote&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;class BoringHolidayException extends Exception {}&lt;br /&gt;&lt;br /&gt;interface Doable {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public void doSomething() throws BoringHolidayException;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;}&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;  &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;class Okay implements Doable {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;//  Não declara a exceção BoringHolidayException.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    @Override&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public void doSomething() {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        System.out.println(&quot;Oi,  sou o método doSomething subscrito em Okay.&quot;);&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    }&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;  &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    public static void  main(String[] args) {&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        Okay o = new Okay();&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        Doable d = o;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;         // Como a declarção de doSomething() na interface&lt;br /&gt;    // declara lançar a&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt; exceção BoringHolidayException,&lt;br /&gt;    // a chamada polimórfica do método&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt; através de uma&lt;br /&gt;    // referência à interface exigirá tratamento das exceções.&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;        &lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;d.doSomething();   // Erro em tempo de compilação.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;   // Enquanto chamar o método diretamente por uma&lt;br /&gt;   // referência Okay não irá lançar a exceção, uma&lt;br /&gt;   // vez que, propositalmente, o método subscritor&lt;br /&gt;   // não lança nenhuma exceção.&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;font-weight: bold;font-family:courier new;font-size:100%;&quot;  &gt;         &lt;span style=&quot;color: rgb(51, 51, 255);&quot;&gt;o.doSomething();   // OK. Sem problemas.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;    }&lt;/span&gt;&lt;span style=&quot;;font-family:courier new;font-size:100%;&quot;  &gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/blockquote&gt;O que já se sabe (se você também já leu o capítulo 1 do mencionado livro) é que duas das regras de subscrição são:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;não declarar exceções mais abrangentes e;&lt;/li&gt;&lt;li&gt;não declarar novas exceções no método subscritor.&lt;/li&gt;&lt;/ol&gt;Embora você nem precise declarar qualquer exceção declarada pelo método subscrito, nunca faça nenhuma dessas duas coisas.&lt;br /&gt;&lt;br /&gt;Então, neste caso, é necessário tratar a exceção declarada pela chamada polimórfica de doSomething(), mas não é necessário se preocupar pela chamada a partir da referência Okay.&lt;br /&gt;&lt;br /&gt;Certo! Fiz esse post, porque sei que a SCJP adora pegadinhas desse tipo, então, tendo escrito aqui, não vou esquecer na hora da prova :ô) e, quem sabe, algum dia, alguém se preocupe em pesquisar isso no Google...</content><link rel='replies' type='application/atom+xml' href='http://jotageek.blogspot.com/feeds/3277222695655845802/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://jotageek.blogspot.com/2010/02/quando-as-chamadas-polimorficas-mudam.html#comment-form' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/3277222695655845802'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/3277222695655845802'/><link rel='alternate' type='text/html' href='http://jotageek.blogspot.com/2010/02/quando-as-chamadas-polimorficas-mudam.html' title='Quando as chamadas polimórficas mudam tudo...'/><author><name>Hiram</name><uri>http://www.blogger.com/profile/07914627338457007583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLcSwtPgc15-EzkwZ73dp93N-5V8N0wnTDmFQ7-lx_0PWMrxqm-77AyNpYbHF4PaLQ7jQB1v_jesQmhs4aoyIfFxcnreihObYnIKJNc3wqBT3d8wCZ7ZPeb3mM_2L0jw/s220/cigarrinhosxn1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5858392062262821415.post-7504906873825217099</id><published>2009-11-20T11:59:00.006-02:00</published><updated>2009-11-28T18:16:02.753-02:00</updated><title type='text'>Resumo do cap. 1 do PMBOK</title><content type='html'>&lt;span style=&quot;font-family: verdana;font-family:verdana;font-size:100%;&quot;  &gt;&lt;i&gt;&lt;b&gt;Resumo PMBOK&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;div style=&quot;font-family: verdana;&quot; face=&quot;trebuchet ms&quot; xmlns=&quot;&quot;&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;Autor: Hiram Castelo Branco T. Nascimento&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Capítulo 1 – A estrutura do gerenciamento de projetos&lt;/b&gt;&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;Introdução&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;O Conjunto de conhecimentos em gerenciamento de projetos é a soma dos conhecimentos intrínsecos à profissão de gerenciamento de projetos.” (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;Objetivos do PMBOK&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;O principal objetivo do  &lt;i&gt;Guia PMBOK&lt;/i&gt;&lt;i&gt;®&lt;/i&gt; é identificar o subconjunto do Conjunto de conhecimentos em gerenciamento de projetos que é amplamente reconhecido como boa prática.” (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&#39;Boa prática&#39; significa que existe acordo geral de que a aplicação correta dessas habilidades, ferramentas e técnicas podem aumentar as chances de sucesso em uma ampla série de projetos diferent&lt;i&gt;e&lt;/i&gt;s.” (PMBOK, 2004)&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; page-break-before: always; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;O que é um projeto?&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Um projeto é um esforço temporário empreendido para criar um produto, serviço ou resultado exclusivo.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-left: 1.25cm; margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Empreender:&lt;/b&gt; Experimentar, procurar fazer, tomar iniciativa de ação, tarefa, realização etc.&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-left: 1.25cm; margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Temporário:&lt;/b&gt;&lt;/i&gt;&lt;i&gt;significa que todos os projetos possuem um início e um final definidos&lt;/i&gt;&lt;i&gt;.&lt;/i&gt;&lt;i&gt;Projetos&lt;/i&gt;&lt;i&gt;&lt;u&gt;não&lt;/u&gt;&lt;/i&gt;&lt;i&gt;são esforços contínuos.&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-left: 1.25cm; margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Produtos, serviços ou resultados exclusivos:&lt;/b&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;cada um desses é uma instância singular.&lt;/span&gt;&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-left: 1.25cm; margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Elaboração progressiva:&lt;/b&gt; significa desenvolver em etapas e continuar por incrementos. Não deve ser confundida com aumento do escopo.&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Observação sobre iterações:&lt;/b&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;“É importante observar que muitos processos dentro do gerenciamento de projetos são iterativos devido à existência, e necessidade, de uma elaboração progressiva em um projeto durante todo o ciclo de vida do projeto.”&lt;/span&gt;&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; font-weight: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;É importante, também, não confundir a necessidade de iterações por erros na definição de escopo (o que é ruim), mas sim pela elaboração progressiva (o que é bom e necessário).&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; font-weight: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;Sabe-se, portanto, que a medida que uma equipe de projetos ganha experiência em um tipo de projeto, todo conhecimento adquirido é útil para uma melhor gerência nos próximos empreendimentos. É importante haver um acervo para armazenar tais conhecimentos adquiridos.&lt;/i&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; page-break-before: always; text-align: justify; font-weight: bold;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Projetos x Operações&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;Os projetos e as operações diferem principalmente no fato de que as operações são contínuas e repetitivas, enquanto os projetos são temporários e exclusivos.” (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-style: normal;&quot;&gt;Os projetos são diferentes&lt;/span&gt; porque o projeto termina quando seus objetivos específicos foram atingidos, enquanto as operações adotam um novo conjunto de objetivos e o trabalho continua.” (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;Projetos e planejamento estratégico&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;Os projetos são um meio de organizar atividades que não podem ser abordadas dentro dos limites operacionais normais da organização. Os projetos são, portanto, frequentemente utilizados como um meio de atingir o plano estratégico de uma organização, seja a equipe do projeto formada por funcionários da organização ou um prestador de serviços contratado.” (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Projetos são, em geral, concebidos e autorizados em resposta a uma das seguintes considerações estratégicas:&lt;/span&gt; &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Demanda de mercado&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Necessidade organizacional&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Requisito legal&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Solicitação de um cliente&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Avanço tecnológico&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;margin-bottom: 0cm; page-break-before: always; text-align: justify; font-weight: bold;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;O que é gerenciamento de projetos?&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;O gerenciamento de projetos é a aplicação de conhecimento, habilidades, ferramentas e técnicas às atividades do projeto a fim de atender aos seus requisitos.”&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Segundo o PMBOK, o gerenciamento de projetos é realizado através da aplicação e da integração dos seguintes processos de gerenciamento de projetos:&lt;/span&gt; &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;iniciação,&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;planejamento,&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;execução,&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;monitoramento e controle, e&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; font-style: normal; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;encerramento.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;span style=&quot;font-style: normal;&quot;&gt;O gerente de projetos é a pessoa&lt;/span&gt; responsável pela realização dos objetivos do projeto.”p  (PMBOK, 2004)&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;“ &lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;Gerenciar um projeto inclui:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Identificação das necessidades&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Estabelecimento de objetivos claros e alcançáveis&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Balanceamento das demandas conflitantes de qualidade, escopo, tempo e custo&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Adaptação das especificações, dos planos e da abordagem às diferentes preocupações e expectativas das diversas partes interessadas.” (PMBOK, 2004)&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;margin-bottom: 0cm; text-align: justify;&quot; class=&quot;western&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;i&gt;&lt;b&gt;Observação sobre qualidade:&lt;/b&gt; “Projetos de alta qualidade entregam o produto, serviço ou resultado solicitado dentro do escopo, no prazo e dentro do orçamento.” (PMBOK, 2004)&lt;/i&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jotageek.blogspot.com/feeds/7504906873825217099/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://jotageek.blogspot.com/2009/11/resumo-do-cap-1-do-pmbok.html#comment-form' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/7504906873825217099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/7504906873825217099'/><link rel='alternate' type='text/html' href='http://jotageek.blogspot.com/2009/11/resumo-do-cap-1-do-pmbok.html' title='Resumo do cap. 1 do PMBOK'/><author><name>Hiram</name><uri>http://www.blogger.com/profile/07914627338457007583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLcSwtPgc15-EzkwZ73dp93N-5V8N0wnTDmFQ7-lx_0PWMrxqm-77AyNpYbHF4PaLQ7jQB1v_jesQmhs4aoyIfFxcnreihObYnIKJNc3wqBT3d8wCZ7ZPeb3mM_2L0jw/s220/cigarrinhosxn1.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5858392062262821415.post-8419522935730380833</id><published>2009-04-12T15:04:00.003-03:00</published><updated>2009-11-28T18:16:13.133-02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="exception"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="nullpointerexception"/><title type='text'>A famigerada java.lang.NullPointerException</title><content type='html'>&lt;div style=&quot;font-family: verdana;&quot; xmlns=&quot;&quot;&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Hoje eu respondi a um post sobre Arrays criado por um usuário de um dos fóruns Java que freqüento.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;A dúvida era simples: como verificar se um elemento do array era nulo. O que me intrigou durante uns testes que eu fiz é que a exceção NullPointerException é lançada em muitos contextos diferentes. Decidi, então, pesquisar sobre mais detalhes dessa &quot;famosa&quot; exceção que, apesar de muito popular, gera dúvidas em programadores Java iniciantes - como eu - sobre em que casos ela é de fato lançada.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;A NullPointerException é uma subclasse da RuntimeException e é lançada sempre que um valor null é fornecido em situações em que um objeto, ou uma referência a um objeto, precisa ser fornecido.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;As situações específicas que causam o lançamento dessa exceção são:&lt;/span&gt; &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;A chamada de um método de instância de um objeto nulo.&lt;/b&gt; Por exemplo, quando um objeto é apenas inicializado e não instanciado. Assim, ele não pode ter um de seus métodos de instância chamado. Por distração, esse problema ocorre frequentemente quando se trabalha com arrays de objetos.&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;O acesso ou tentativa de modificação de um atributo de um objeto nulo.&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Os outros três principais casos merecem exemplos em código:&lt;/span&gt; &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;A tentativa de se tomar o tamanho de um objeto nulo, ou seja, com referência a null, como se ele fosse um array.&lt;/b&gt;&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;&lt;b&gt;String&lt;/b&gt; str =  &lt;i&gt;null&lt;/i&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;&lt;b&gt;int&lt;/b&gt; l;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;l = str.length();&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;As tentativas de acesso ou modificação os elementos de um objeto nulo como se ele fosse um array.&lt;/b&gt; Talvez essa seja a causa mais comum. Segue o exemplo:&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;&lt;b&gt;String&lt;/b&gt; str =  &lt;i&gt;null&lt;/i&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;str[2] =  &lt;i&gt;&quot;vai dar problema!&quot;&lt;/i&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;ul&gt;&lt;li&gt;&lt;p style=&quot;line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;b&gt;O lançamento de um objeto nulo como se ele fosse uma subclasse de Throwable.&lt;/b&gt; Veja:&lt;/span&gt; &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;blockquote style=&quot;margin-left: 2.5cm; line-height: 150%; text-align: justify;&quot;&gt;&lt;span style=&quot;;font-size:100%;&quot; &gt;&lt;span style=&quot;font-size:11pt;&quot;&gt;&lt;b&gt;if&lt;/b&gt;(args.length == 0)  &lt;b&gt;throw&lt;/b&gt;&lt;i&gt;null&lt;/i&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Para não dizer que não falei de flores, o tipo null, segundo as especificações da JVM abstrata, isto é, sem distinção de implementações da JVM, está intimamente relacionado a referências. Ele é nada mais nada menos que uma referência nula, ou a nada.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;A documentação da Java API aconselha que as aplicações deveriam lançar a exceção NullPointerException nos demais casos em que um valor null é fornecido em lugar de um objeto.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;Em caso de dúvidas postem comentários.&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm; line-height: 150%; text-align: justify;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;img style=&quot;&quot; src=&quot;https://blogger.googleusercontent.com/tracker/5858392062262821415-8419522935730380833?l=jotageek.blogspot.com&quot; name=&quot;figura1&quot; border=&quot;0&quot; height=&quot;2&quot; width=&quot;2&quot; /&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;margin-bottom: 0cm;&quot;&gt; &lt;span style=&quot;font-size:100%;&quot;&gt;&lt;img style=&quot;&quot; src=&quot;https://blogger.googleusercontent.com/tracker/5858392062262821415-8419522935730380833?l=jotageek.blogspot.com&quot; name=&quot;figura2&quot; border=&quot;0&quot; height=&quot;2&quot; width=&quot;2&quot; /&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jotageek.blogspot.com/feeds/8419522935730380833/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://jotageek.blogspot.com/2009/04/famigerada-javalangnullpointerexception.html#comment-form' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/8419522935730380833'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5858392062262821415/posts/default/8419522935730380833'/><link rel='alternate' type='text/html' href='http://jotageek.blogspot.com/2009/04/famigerada-javalangnullpointerexception.html' title='A famigerada java.lang.NullPointerException'/><author><name>Hiram</name><uri>http://www.blogger.com/profile/07914627338457007583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLcSwtPgc15-EzkwZ73dp93N-5V8N0wnTDmFQ7-lx_0PWMrxqm-77AyNpYbHF4PaLQ7jQB1v_jesQmhs4aoyIfFxcnreihObYnIKJNc3wqBT3d8wCZ7ZPeb3mM_2L0jw/s220/cigarrinhosxn1.jpg'/></author><thr:total>2</thr:total></entry></feed>